是“onload”吗?当代码位于底部时有必要吗?
我想知道 window.onload = function(){}
(或任何其他类型的 onload,如 jQuery $(document).ready();
是否必要如果代码放在我的 的底部?
或者可能会出现非常意外的副作用?
I was wondering if the window.onload = function(){}
(or any other kind of onload, like the jQuery $(document).ready();
is necessary if the code is placed at the bottom of my <body>
?
Or there could be highly unexpected side-effects?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,可能会出现意想不到的后果。但是,不,这不是绝对必要的。对于仍在加载的内容(例如复杂的布局、深层 DOM 结构、来自其他脚本的动态 HTML 或图像),时机可能已关闭。为了避免这些情况,将脚本包装在
onload
事件中始终是最安全的。以下是一些例子来证明这一点。所有示例均在 OS X 上的 Chrome 17.0.963.12 开发版上进行测试。不使用
onload
时,浏览器结果可能会有所不同,这表明了其不可预测的行为。如果结果与您的预期(即您的设计指定的)不同,这些示例将返回fail
;当结果与您的预期相符时,这些示例将返回success
。使用onload
,它们总是返回成功
。示例 1
在此示例中,代码期望图像具有特定宽度。如果代码包含在
onload
事件中,则宽度是正确的,否则宽度是不正确的。演示:http://jsfiddle.net/ThinkingStiff/qUWxX/
HTML:
脚本:
您会看到jsFiddle 设置为页面左上角的“onLoad”,图像上方的结果是
成功
。将其更改为“onDomReady”或“无换行(正文)”:
现在按页面左上角的“运行”:
图像上方的结果现在将是
失败。
示例 2
这是另一个不使用图像的示例。在此示例中,内联脚本已添加到 HTML 中。该代码期望宽度是内联脚本设置的宽度。使用
onload
是正确的,不使用则不是。对于此演示,请使用与之前相同的说明。演示: http://jsfiddle.net/ThinkingStiff/n7GWt/
HTML:
脚本:
示例 3
这是一个正文中不使用图像或 Javascript,仅使用 CSS 的示例。同样,
onload
和 not 之间的结果是不同的。演示: http://jsfiddle.net/ThinkingStiff/HN2bH/
CSS:
HTML:
脚本:
有在很多情况下,不包装代码可能会导致您无法预料的问题。为了避免这些情况,将脚本包装在
onload
事件中始终是最安全的。Yes, there could be unexpected consequences. But, no, it's not absolutely necessary. The timing could be off for things still loading, like complicated layouts, deep DOM structures, dynamic HTML from other scripts, or images. To avoid these situations, it's always safest to wrap your script in an
onload
event.Here are some examples that demonstrate this. All examples tested on Chrome 17.0.963.12 dev on OS X. Browser results may vary when not using
onload
, which demonstrates its unpredictable behavior. The examples returnfail
if the result is different than what you'd expect (i.e. what your design specifies) and returnsuccess
when the result matches what you would expect. Withonload
they always returnsuccess
.Example 1
In this example, the code is expecting the image to be a certain width. If the code is wrapped in an
onload
event the width is correct, otherwise, it's not.Demo: http://jsfiddle.net/ThinkingStiff/qUWxX/
HTML:
Script:
You'll see the jsFiddle is set to "onLoad" in the upper left corner of the page and the result above the image is
success
.Change that to "onDomReady" or "no wrap (body)":
Now press "Run" at the top left of the page:
The result above the image will now be
fail
.Example 2
Here is another example that doesn't use images. In this one, an inline script has been added to the HTML. The code is expecting the width to be what it was set to by the inline script. With
onload
it's corrent, without, it's not. Use the same instructions as before for this demo.Demo: http://jsfiddle.net/ThinkingStiff/n7GWt/
HTML:
Script:
Example 3
Here's an example that uses no images or Javascript in the body, just CSS. Again, the results are different between
onload
and not.Demo: http://jsfiddle.net/ThinkingStiff/HN2bH/
CSS:
HTML:
Script:
There are just too many scenarios where not wrapping your code can cause issues that you won't be able to anticipate. To avoid these situations, it's always safest to wrap your script in an
onload
event.发生了一些不同的事情。
onload
仅在加载图像等嵌入内容后调用。这意味着您可以将代码放入onload
中,具体取决于该内容。从技术上讲,在文档末尾运行的脚本可以使用 getElementById 等方法来提取已呈现的元素。由于上述原因,您可能仍然希望将它们放入就绪或加载处理程序中。这并不是说脚本本身不应该位于底部 - 将它们放在底部仍然有利于感知性能。
Couple of different things going on.
onload
is called only after embedded content such as images is loaded. This means you can put code inonload
that depends on that content being there.Technically scripts that run at the end of the document can use methods like
getElementById
to pull in elements that are already rendered. You may still want to put those in a ready or load handler for the above reasons. This isn't to say the scripts themselves shouldn't be at the bottom - there's still a benefit to perceived performance from having them there.HTML 页面底部的脚本标记相当于
DOMContentLoaded
。所有 html 代码都已下载,Javascript 现在可以访问 DOM 元素了。当所有其他资源(例如图像)完全下载时调用
load
。A script tag at the bottom of an HTML page is equivalent to
DOMContentLoaded
. All the html code has been downloaded, and Javascript is now capable of accessing DOM elements.load
is called when all other resources, such as images, have completely downloaded.