简体   繁体   中英

Passing Variables in Javascript - Doesnt Execute

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>

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>

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM