javascript <script defer> 和 $(document).ready

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8638551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 04:04:25  来源:igfitidea点击:

<script defer> and $(document).ready

javascriptjqueryhtml

提问by All Workers Are Essential

According to http://caniuse.com/script-defer, most browsers support the scripttag's deferattribute.

根据http://caniuse.com/script-defer,大多数浏览器都支持script标签的defer属性。

I would like to know if scripts specified by <script defer src="...">get executed before or after jQuery's $(document).ready()? Are the major, modern browsers (Chrome, Firefox, IE, etc.) consistent in the order of execution or does it vary?

我想知道由<script defer src="...">jQuery指定的脚本是在 jQuery 之前还是之后执行$(document).ready()?主要的现代浏览器(Chrome、Firefox、IE 等)在执行顺序上是一致的还是有所不同?

采纳答案by Telmo Marques

Based on this fiddleI have to say jQuery's $(document).ready()executes aftera script declared with defer. I tested it with Firefox and Chrome, and both had the same behavior independently of the sequence of the scripts.

基于此 小提琴我不得不说jQuery的$(document).ready()执行后,一个脚本声明defer。我用 Firefox 和 Chrome 对其进行了测试,它们的行为与脚本顺序无关。

I guess behavior on other browsers might change based on their implementation, so it's always uncertain.

我猜其他浏览器上的行为可能会根据它们的实现而改变,所以它总是不确定的。

EDIT: As it turns out, the deferattribute should be used with an external javascript file. I edited the fiddle to show this, apparently with the same results.

编辑:事实证明,该defer属性应该与外部 javascript 文件一起使用。我编辑了小提琴以显示这一点,显然结果相同。

Updated fiddle here: http://jsfiddle.net/RNEZH/15/

在这里更新小提琴:http: //jsfiddle.net/RNEZH/15/

回答by Larry K

The defer attribute has a good write-up and analysis. AlsoSee the comments to the post for additional info on how deferhas been re-defined in HTML5.

defer 属性有很好的写作和分析请参阅帖子的评论以获取有关如何defer在 HTML5 中重新定义的其他信息。

My conclusion: deferis too browser dependent to count on. Therefore use the jQuery doc ready technique.

我的结论defer是:太依赖浏览器而不能指望。因此,请使用 jQuery doc ready 技术。

To put it another way, an important reason for jQuery is to cover browser inconsistencies. Defer is another such inconsistency that should be avoided for well written pages.

换句话说,jQuery 的一个重要原因是为了覆盖浏览器的不一致。Defer 是另一个这样的不一致,对于写得好的页面应该避免。

回答by Muhammad Usman

Simply, scriptshould be executed before $(document).ready()whether deferis used or not and almost all major browsers supportdefer.

简单来说,无论是否使用script都应该执行,并且几乎所有主流浏览器都支持.$(document).ready()deferdefer

But for being safe side I encourage you to use both $(document).ready()and defer. So why defer? Because it helps page appear quickly (as external script is loaded parallel) and a really important factor in Google's page speed tool, a good detail can be found here http://code.google.com/speed/page-speed/docs/payload.html#DeferLoadingJS

但为了安全起见,我鼓励您同时使用$(document).ready()defer。那为什么defer?因为它有助于页面快速显示(因为外部脚本是并行加载的)并且是 Google 页面速度工具中一个非常重要的因素,所以可以在此处找到一个很好的详细信息http://code.google.com/speed/page-speed/docs/ payload.html#DeferLoadingJS