1

I have written following code to pass File1 variable to Javascript, but its not executing, and I'm not sure why. When I use alert with File1, it works - but the document.write script is not working. Any help?

<script type="text/javascript">
var Order[0]="1";
var Order[2]="2";
var Order[3]="4";
var File1=Order[2]+"/"+Order[0]+"/"+%Order[4];

document.write("<script type='javascript' src=http://abc.com/i_sale_third/10957/'" + File1 + ">";

</script>
2
  • It's not really clear what you're trying to accomplish here. Can you state what you would like to happen more explicitly? Commented Apr 24, 2012 at 3:54
  • The order variables i am going to get from some other JS.. i want to Pass it to the JAvascript tracker code Commented Apr 24, 2012 at 3:56

2 Answers 2

3

Well, you did mess up your single quotes in the document.write. I fixed it in this, see if it works:

<script type="text/javascript">
var Order[0]="1";
var Order[2]="2";
var Order[3]="4";
var File1=Order[2]+"/"+Order[0]+"/"+%Order[4];

document.write("<script type='javascript' src='http://abc.com/i_sale_third/10957/" + File1 + "'>";

</script>

Edits. Try the following code.

Maybe the little % is messing it up too right before Order[4] (and beside the fact that you may not have defined Order[4]). I also added the console.log to your code so open up your console (in Chrome and Safari it is dev tools). You also didn't need to repeat var keyword (you can separate them by commas if you didn't know) and according to @ajax333221 (and me) you need to initialize Order by doing Order = []:

<script type="text/javascript">
var Order = [],
    Order[0] = "1",
    Order[2] = "2",
    Order[3] = "4",
    File1 = Order[2] + "/" + Order[0] + "/" + Order[3]; // I think you meant Order[3] not Order[4] here

if(console) console.log(File1); // this will print File1 into the console so you can see the string output

document.write("<script type='javascript' src='http://abc.com/i_sale_third/10957/" + File1 + "'></script>");
</script>
Sign up to request clarification or add additional context in comments.

9 Comments

A single quote is missing as Nathan pointed out and maybe this is not th case but is Order[4] defined? Also add a console.log to show the value of File. Then check if that file exists by prepending 'abc.com/i_sale_third/10957/'to it and paste it in the browser URL to see if that file loads.
@saganbyte and also there was another single quote messed up, it was right before " + File1 + so the rest was getting cut off in the HTML output when the browser renders it.
I also wonder what the % thing is there for right before Order[4].
Also why is 'var' repeated for every 'Order[]'
also as far as I know, you need to initialize the array first var Order=[] before you can do any Order[0]="x";
|
1

try with this:

<script type="text/javascript">
    var Order = []; //created the array
    Order[0] = "1";
    Order[2] = "2";
    Order[3] = "4";

    var File1 = Order[2] + "/" + Order[0] + "/" + Order[3]; //added var and changed Order[3]

    document.write("<script type='javascript' src='http://abc.com/i_sale_third/10957/" + File1 + "'></script>"); //fixed quotes placement and closed with </script>
</script>

Useful links to read:

6 Comments

You don't need the var keyword on File1 since you can define them all with the same var keyword but separating with commas. Isn't this just a copy of my answer [kinda]? ;) And you can't end <script> with /> as far as I know.
@Nathan it is not a copy, my differs a lot. I used ; after every Order[x]=x (while you used commas), I closed properly document.close() and other major improvements. About the /> I think you are right
Oh yeah that's an error on my side I forgot that when you use commas, it is really putting var in front of all them.
I've never heard of document.close(), what is the purpose of it?
@Nathan I remember I once read about it on stackoverflow, but for now the only link I can provide is this javascript.gakaa.com/document-close-4-0-5-.aspx
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.