最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - HTML parse orderscript execution order - Stack Overflow

programmeradmin5浏览0评论

the following lines are from the official jQuery website

<script src=".4.2/jquery.min.js"></script>
<script>!window.jQuery && document.write('<script src=".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?

the following lines are from the official jQuery website

<script src="http://ajax.googleapis./ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>!window.jQuery && document.write('<script src="http://code.jquery./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?

Share Improve this question asked Aug 13, 2011 at 18:01 kaekae 731 gold badge1 silver badge3 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 12

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.

Yes it will wait with parsing any html until the script is loaded and parsed. This is one reason why you should load externa scripts at the end of your page so script will not blocking the html rendering.

Lets talk about the more traditional way of handling without using attribute defer or async

The browsers will:

  1. Download scripts, blocking other resource from downloading

  2. Parse the scripts

  3. Execute the scripts

See Ch1, Loading and Executing from High Performance JavaScript

Here's another good reference 12.3. Execution of JavaScript Programs from JavaScript: The Definitive Guide, 4th Edition

发布评论

评论列表(0)

  1. 暂无评论