Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML parse order / script execution order

the following lines are from the official jQuery website

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>!window.jQuery && document.write('<script src="http://code.jquery.com/jquery-1.4.2.min.js"><\/script>');</script>

I'm not sure about the HTML parse order, or should I say script execution order.

The question is: Will line 2 wait for line 1 to load? I doubt it.

If line 1 is still being loaded (say it's 3000KB, and takes long), and line 2 is executed already. window.jQuery would always be false, and so the second js is always included. If that's true, what is line 1 for anyway?

like image 861
kae Avatar asked Aug 13 '11 18:08

kae


1 Answers

The scripts are executed in the order they are in the document. The browser waits for the script to load before executing the scripts following it.

If this weren't the case, you couldn't have any two files be dependent of each other. You'd have to put everything in the same file because otherwise the script execution order would be practically random.

like image 188
JJJ Avatar answered Nov 08 '22 04:11

JJJ