Are external javascript files loaded sequentially or in parallel?

if there are several script tags on my page:

<script src="js/jquery-1.7.min.js" type="text/javascript"></script> <script src="js/jquery.jplayer.min.js" type="text/javascript"></script> <script src="js/globals.js" type="text/javascript"></script> <script src="js/sound.js" type="text/javascript"></script> 

Is it possible to rely on the fact that the code from the previous ones is already available when the latter are loaded?

+7
source share
4 answers

In general, scripts are loaded sequentially (see this page ):

Because JavaScript code can modify the content and layout of a page’s website, the browser delays the rendering of any content following the script until the script is loaded, parsed, and executed. However, more importantly for temporary disabling, many browsers block the loading of resources [such as stylesheets, images, and other scripts] referenced in the document after the scripts until these scripts are downloaded and executed.

+5
source

They can be downloaded (via the network) in parallel, but they are evaluated sequentially.

So yes, you can rely on the order.

+7
source

Short: Yes:

Without specifying defer or async properties in the script tag, the specification says that the browser must sequentially (synchronize) the loading of these files.

In other words, the regular script tag that the browser finds should get

  • loaded
  • performed
  • (block any other rendering / execution process while doing the above)

While the β€œmodern” browser is probably still trying to optimize this process, these steps need to be applied (at least for the process). That is why you should place script tags without additional specification always at the bottom of the <body> . Even the DOM rendering process stops when loading / executing scripts.

To avoid this, you can specify the defer or async property (HTML5 only) in script tags, e.g.

 <script defer src="/foo/bar.js"></script> 

which tells the browser that this is a script that should have been executed after parsing the document.

See https://developer.mozilla.org/En/HTML/Element/Script

+5
source

They are loaded in parallel, but they only start after each file is downloaded. So the answer is yes, you can rely on this fact.

+3
source

All Articles