HTML解析顺序/脚本执行顺序 [英] HTML parse order / script execution order

查看:171
本文介绍了HTML解析顺序/脚本执行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下几行来自jQuery官方网站

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>

我不确定HTML解析顺序,还是应该说脚本执行顺序.

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

问题是:第2行会等待第1行加载吗?我对此表示怀疑.

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

如果第1行仍在加载(例如3000KB,需要很长时间),并且第2行已经执行. window.jQuery始终为false,因此始终包含第二个js.如果是这样,那么第1行到底是什么?

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 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.

这篇关于HTML解析顺序/脚本执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆