JavaScript 延迟

发布于 2024-11-02 14:57:27 字数 141 浏览 1 评论 0原文

在关闭 标记之前放置

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情泪▽动烟 2024-11-09 14:57:27

是/否。

是的,因为放置延迟标记会等到文档加载后才执行。

否,因为将

<html>
  <head>
  </head>
  <body>
  <script>...</script>
  </body>
  <link/>
  <script>
  although it is invalid HTML most browsers will render tags outside the body. This is 
  probably more of an error in code
  <div> some content</div>
</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. Example

<html>
  <head>
  </head>
  <body>
  <script>...</script>
  </body>
  <link/>
  <script>
  although it is invalid HTML most browsers will render tags outside the body. This is 
  probably more of an error in code
  <div> some content</div>
</html>

Also 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

GRAY°灰色天空 2024-11-09 14:57:27

defer 需要 Gecko 1.9.1 这个
布尔属性设置表示
向浏览器发送该脚本的含义
在文件完成后执行
被解析了。由于这个功能还没有
尚未被所有其他实施
主要浏览器,作者不应该
假设脚本的执行
实际上会被推迟。从不打电话
来自延迟脚本的 document.write()
(从 Gecko 1.9.2 开始,这会爆炸
带走文档)。推迟
不应在脚本上使用属性
没有 src 属性。
从 Gecko 1.9.2 开始,defer 属性
在没有的脚本上被忽略
src 属性。然而,在壁虎
1.9.1 如果设置了 defer 属性,甚至内联脚本也会被延迟。

文档解析完成时和 标签末尾的情况类似,但不完全相同。

同样重要的是要注意,这只适用于具有外部 src 集的脚本。

defer Requires Gecko 1.9.1 This
Boolean attribute is set to indicate
to a browser that the script is meant
to be executed after the document has
been parsed. Since this feature hasn't
yet been implemented by all other
major browsers, authors should not
assume that the script’s execution
will actually be deferred. Never call
document.write() from a defer script
(since Gecko 1.9.2, this will blow
away the document). The defer
attribute shouldn't be used on scripts
that don't have the src attribute.
Since Gecko 1.9.2, the defer attribute
is ignored on scripts that don't have
the src attribute. However, in Gecko
1.9.1 even inline scripts are deferred if the defer attribute is set.

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.

晨敛清荷 2024-11-09 14:57:27

一般来说是的,但是浏览器不保证它们会在加载页面后执行 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").

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文