onLoad 总是在 DOM 准备好后被触发吗?
我想在 DOM 准备好时禁用我的插件对 DOM 执行的某些特定“功能”。
如果我将代码放入 onload
回调中,它是否总是在 ready
回调之后执行?
我想百分百确定,即使没有任何图像,也只会在加载之前执行准备好的图像。
I want to disable some specific "feature" that one my plugins does to the DOM when the DOM is ready.
If I put my code inside the onload
callback, will it always get executed after the ready
callback?
I want to be sure in 100% that even if there aren't any images of just few the ready will be executed before the onload.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
虽然我认为 @jAndy 的答案 是正确的,但您也可以通过将代码放在
中来为自己提供一些额外的保证onload
在ready()
调用中。只需确保您的主要
ready()
调用首先出现。因此,如果在
onload
触发时ready()
已经触发,您的onload
代码仍将运行。 (一旦 DOM 准备好,就会设置一个内部标志,因此将来的.ready()
调用将立即被调用。)如果
.ready()
> 在发生onload
时未以某种方式触发,这意味着您的原始ready()
代码位于内部数组中的第一个,并且将添加新的.ready()
代码到数组的末尾。编辑:
查看在 DOM 准备就绪时触发的主
jQuery.ready
处理程序的源代码(反过来,触发用户的列表).ready()
handlers),看起来好像有一个 IE bug,处理程序触发得有点早。为了修复此错误,jQuery 使处理程序异步调用,直到它实际看到
document.body
。看来,由于处理程序的异步循环,IE 至少可能倾向于在
.ready()
处理程序之前调用window.onload
处理程序。按照我上面的描述,添加到
onload
中的.ready()
处理程序列表应该可以解决这个问题。While I think @jAndy's answer is right, you can afford yourself some extra assurance by also placing the code in the
onload
within aready()
call.Just make sure that your main
ready()
call comes first.So if the
ready()
has already fired by the time theonload
fires, youronload
code will still run. (An internal flag is set once the DOM is ready, so future.ready()
calls are immediately invoked.)If the
.ready()
has somehow not fired when theonload
happens, that will mean that your originalready()
code is first in the internal Array, and the new.ready()
code will be added to the end of the Array.EDIT:
Looking at the source for the main
jQuery.ready
handler that is fired when the DOM is ready (which in turn, fires the list of the user's.ready()
handlers), it appears as though there's an IE bug where the handler fires a little early.To remedy this bug, jQuery makes the handler asynchronously invoked until it can actually see
document.body
.It would seem that because of this asynchronous looping of the handler, then IE would at least be possibly prone to having the
window.onload
handler invoked before the.ready()
handlers.Adding to the
.ready()
handler list within theonload
as I described above should remedy this.是的。
onload
将在加载任何其他内容(图像、框架、iframe 等)时触发,DOMContentLoaded
将在 DOM 树可以访问时立即触发。Yes.
onload
will fire when anything else was loaded (images, frames, iframes, etc.),DOMContentLoaded
will fire as soon as the DOM tree can get accessed.不。亲自尝试一下:创建一个大型 HTML 文件,删除所有依赖资源(图像、脚本、样式表)并进行测试。
当从互联网获取每个资源时,加载会触发。 DOMContentLoad 在获取 HTML 并对其进行解析时触发。在我的测试中,后者使用 140 KB HTML 代码花费了 14 毫秒。当然,这是一种边缘情况,但不能保证 load 在 DOMContentLoaded 之前触发 - 即使使用外部资源也不行(由于缓存、使用 HTTP/2 并行下载、快速连接)。
No. Just try it out for yourself: Create a large HTML file, strip every dependent resource (images, scripts, stylesheets) and test.
Load fires when every resource is fetched from the internet. DOMContentLoad fires when the HTML is fetched and parsed. The latter took 14 ms in my test with 140 KB HTML code. Sure, that's an edge case, but there is no guarantee that load fires before DOMContentLoaded - not even with external resources (due to caching, parallel downloads with HTTP/2, fast connections).