Reputation: 10994
If I have an HTML page embedding several <script>
tags, is there a guarantee that these script files will be executed in order they appear in the HTML file?
For example:
<script src="script1"></script>
<script src="script2"></script>
Is it guaranteed that script1
will always be executed before script2
? If so, is it a cross-browser standard?
Upvotes: 1
Views: 1567
Reputation: 8233
Those JS files will be parsed and interpreted by browser in the order you putted them : script1.js, then script2.js.
However, it doesn't garanty that the code in script1.js
will be executed before script2.js
: it depends on the code.
For example, let's consider you are listening to a DOM ready event in script2.js
and you are listening to an image loaded event in script1.js
. In this case, the code in script2.js
will be parsed and interpreted after the code in script1.js
, but it will be executed after.
Upvotes: 0
Reputation: 746
The answer is yes. The browser will download both scripts in parallel and execute them as soon as possible, maintaining their order. Here's a handfull article that treats this subject.
Upvotes: 2