John Hoffman
John Hoffman

Reputation: 18627

Will <script> tags be blocking and execute one after another?

In the code below, will bar.js only run after foo.js had loaded and run?

<!DOCTYPE html>
<html>
<body>
  <script src='foo.js'></script>
  <script src='bar.js'></script>
</body>
</html>

Assuming I don't use async or deferred, I think so. However, this Q insists that scripts that programmatically set src do not block - does that not apply to literally inserted <script> tags?

Upvotes: 2

Views: 814

Answers (2)

Sgt AJ
Sgt AJ

Reputation: 863

According to this page (https://www.safaribooksonline.com/library/view/even-faster-web/9780596803773/ch04.html), external .js files loaded with src will indeed be run sequentially, and this is intended behavior. Older browsers will download the external files sequentially, then run them sequentially, while newer browsers will download them in parallel (simultaneously), but still run them sequentially. According to the answers on this page (Implications of multiple <script> tags in HTML), inline tags also run sequentially. The first web site linked shows several ways to get multiple external scripts to run simultaneously, however, if that is your desired behavior.

Upvotes: 5

Klapaucjusz TF
Klapaucjusz TF

Reputation: 181

No it doesn't apply.

'script' tags in HTML code always block.

Reference: High Performance Javascript, 1. -> Script Positioning

Upvotes: 1

Related Questions