JavaScript 延迟
在关闭 标记之前放置
标记与将它们放置在
部分中相同并指定一个
defer="defer"
属性?
Is placing <script>
tags in just before closing the <body>
tag the same sa placing them in the <head>
section and specifying a defer="defer"
attribute?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是/否。
是的,因为放置延迟标记会等到文档加载后才执行。
否,因为将
放在
标记之前并不一定意味着文档已完全加载,因为您可以在结束正文之间添加其他标记标签和结束 HTML 标签。示例
另外值得注意的是,script 标记的 defer 属性并非在所有浏览器中都起作用。
已编辑:
关于更快加载页面的性能,您可能需要查看这篇文章,它提供了一些指南,包括放置脚本和 CSS 的位置
http://developer.yahoo.com/performance/rules.html
Yes/No.
Yes because placing the defer tag waits until the document is loaded before executing.
No because placing the
<script>
before the</body>
tag doesn't necessarily mean the document is completely loaded as you can have other tags between the closing body tag and the closing HTML tag. ExampleAlso of note, the defer attribute of the script tag is not functional in all browsers.
Edited:
In regards to performance for faster loading pages you may want to look at this article it provides some guidelines including where to put script and css
http://developer.yahoo.com/performance/rules.html
文档解析完成时和
标签末尾的情况类似,但不完全相同。
同样重要的是要注意,这只适用于具有外部
src
集的脚本。When the document has finished parsing and at the end of the
<body>
tag are similar but not the exact same.It's also important to note this only works for scripts with an external
src
set.一般来说是的,但是浏览器不保证它们会在加载页面后执行 JavaScript,除非您指定(defer="defer")。
Generally yes, but the browsers do not guarantee they will execute JavaScript after loading the page unless you specify so (defer="defer").