< script defer>和$(document).ready [英] <script defer> and $(document).ready

查看:116
本文介绍了< script defer>和$(document).ready的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我想知道< script defer src =...> 在jQuery的 $(document).ready()?主要的现代浏览器(Chrome,Firefox,IE等)按执行顺序保持一致还是会有所不同?

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?

推荐答案

<基于这个 fiddle 我不得不说jQuery的 $(document).ready()在脚本之后执行 defer 声明。我测试了Firefox和Chrome,两者都具有相同的行为,与脚本的顺序无关。

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

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

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

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

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

更新提琴: http://jsfiddle.net/RNEZH/15/

这篇关于&lt; script defer&gt;和$(document).ready的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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