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>
anonymous
  • 1,111
  • 1
  • 10
  • 18
user580950
  • 3,558
  • 12
  • 49
  • 94
  • It's not really clear what you're trying to accomplish here. Can you state what you would like to happen more explicitly? – driangle Apr 24 '12 at 03:54
  • The order variables i am going to get from some other JS.. i want to Pass it to the JAvascript tracker code – user580950 Apr 24 '12 at 03:56

2 Answers2

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>
Nathan
  • 11,814
  • 11
  • 50
  • 93
  • 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 'http://abc.com/i_sale_third/10957/'to it and paste it in the browser URL to see if that file loads. – walmik Apr 24 '12 at 03:59
  • @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. – Nathan Apr 24 '12 at 04:00
  • I also wonder what the `%` thing is there for right before `Order[4]`. – Nathan Apr 24 '12 at 04:01
  • 2
    Also why is 'var' repeated for every 'Order[]' – walmik Apr 24 '12 at 04:06
  • 1
    also as far as I know, you need to initialize the array first `var Order=[]` before you can do any `Order[0]="x";` – ajax333221 Apr 24 '12 at 04:10
  • @ajax333221 Yeah I updated my answer with all that. Hopefully that'll work for them. – Nathan Apr 24 '12 at 04:13
  • "); is missing at the very end – walmik Apr 24 '12 at 04:14
  • @Nathan you sneakily added `>` like my answer :(, well I forgive you because I also sneakily added the console.log thing by mistake – ajax333221 Apr 24 '12 at 04:39
  • @ajax333221 Yeah but it was needed anyway (I didn't really copy it since I noticed that I forgot it.) I upped your answer though since it is still good :) – Nathan Apr 24 '12 at 19:21
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:

Community
  • 1
  • 1
ajax333221
  • 11,436
  • 16
  • 61
  • 95
  • 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 ` – Nathan Apr 24 '12 at 04:20
  • @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 – ajax333221 Apr 24 '12 at 04:22
  • 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. – Nathan Apr 24 '12 at 04:25
  • I've never heard of `document.close()`, what is the purpose of it? – Nathan Apr 24 '12 at 04:26
  • @Nathan I remember I once read about it on stackoverflow, but for now the only link I can provide is this http://javascript.gakaa.com/document-close-4-0-5-.aspx – ajax333221 Apr 24 '12 at 04:27
  • `document.close()` is only when replacing the whole content: _If a script uses document.write( ) or document.writeln( ) to generate **all-new content for a window or frame**, you must append a document.close( ) method to make sure the entire content is written to the document._ – Nathan Apr 24 '12 at 04:33