views:

39

answers:

3

Assume, for whatever reason, that we have three inline scripts on a page. Each of these scripts are functionally the same, and all three will create a script tag, with the source set to a server-side script (each with slightly different parameters) which returns JS to execute. Assume this JS contains a global variable containing important parameters for the function it calls at the end.

var g_important = {"arbitrary1":"someval1", "arbitrary2":"someval2"};
doSomething();

The script in question resides on a CDN with a load balancer. Assume that each request from our original JS could actually be sent to a different server. The response time from the server is now variable, meaning we might get our response to the second request first. That's not a problem because we send a reference value back with the code, ensuring that we're at least working with the specified object.

My question is: what happens when we get two responses at roughly the same time? This is a very unlikely case, but if the first response is in the process of executing doSomething() when the second response comes in, what does the browser do? Does it parse the value for the variable and push the second doSomething call onto the stack?

Assuming this is the case, doSomething could be working with the wrong set of values in theory. I can't recreate this case, as the timing would have to be absolutely perfect, but I need to know if it's a problem.

+3  A: 

Javascript operates "one line at a time". Scripts will always be stacked if another script is running.

And technically, 2 scripts cannot be loaded at the same time, so 1 will be executed before the other.

digitalFresh
Both answers deserve a bump, but I can't give them yet. Scripts are downloading in parallel, but execution order doesn't matter so long as we're not overwriting the variable, which won't be possible since the scripts don't execute in parallel.
Soup d'Campbells
A: 

It depends on how you inject the script. Techniques vary in success of ensuring the order of execution. The common and simple document.createElement + head.appendChild way will not ensure order in IE for example.

more info at Load script without blocking

galambalazs
Very useful link, so thanks. Luckily, as I mentioned above, the execution order doesn't matter, and the scripts don't execute in parallel (meaning the variable assignment won't occur until the browser is ready to, and the appropriate function is called immediately after the assignment).
Soup d'Campbells
A: 

So you're worried that the g_important object could change during the execution of doSomething()? I don't think this is possible, but if you're worried about it, why not register doSomething() as a callback on the completion of each AJAX request. Then each call to the function gets passed the data for its request and never the others.

Maybe I don't fully understand the situation, but this kind of thing would seem to be easily avoidable, if its even possible.

jasongetsdown
It's an existing system, and if I wanted to change the way it is now I would be required to prove the possibility of a problem. However, as digitalFresh pointed out, there are no browsers which execute scripts in parallel, which would be the only case where this could happen (via threading or interrupts in the browser itself).I'll still make the case for moving the doSomething() call to the onload event of the script tag, but it's not necessary with the way browsers currently handle scripts.
Soup d'Campbells