onLoad 总是在 DOM 准备好后被触发吗?

发布于 2025-01-04 09:53:19 字数 161 浏览 0 评论 0原文

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

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

发布评论

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

评论(3

暮倦 2025-01-11 09:53:19

虽然我认为 @jAndy 的答案 是正确的,但您也可以通过将代码放在 中来为自己提供一些额外的保证onloadready() 调用中。

只需确保您的主要 ready() 调用首先出现。

$(document).ready(function() {
    // Your normal ready handler
});

$(window).load(function() {
    // Placing code in another .ready() call in here will add it to the
    //    end of internal Array of ready handlers if any are pending
    $(document).ready(function() {
        // my onload code
    });
});

因此,如果在 onload 触发时 ready() 已经触发,您的 onload 代码仍将运行。 (一旦 DOM 准备好,就会设置一个内部标志,因此将来的 .ready() 调用将立即被调用。)

如果 .ready() > 在发生 onload以某种方式触发,这意味着您的原始 ready() 代码位于内部数组中的第一个,并且将添加新的 .ready() 代码到数组的末尾。


编辑:

查看在 DOM 准备就绪时触发的主 jQuery.ready 处理程序的源代码(反过来,触发用户的列表) .ready() handlers),看起来好像有一个 IE bug,处理程序触发得有点早。

为了修复此错误,jQuery 使处理程序异步调用,直到它实际看到 document.body


ready: function (wait) {
    // Either a released hold or an DOMready/load event and not yet ready
    if ((wait === true && !--jQuery.readyWait) || (wait !== true && !jQuery.isReady)) {

        // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
        if (!document.body) {
            return setTimeout(jQuery.ready, 1);
        }

        // Remember that the DOM is ready
        jQuery.isReady = true;

        // If a normal DOM Ready event fired, decrement, and wait if need be
        if (wait !== true && --jQuery.readyWait > 0) {
            return;
        }

        // If there are functions bound, to execute
        readyList.fireWith(document, [jQuery]);

        // Trigger any bound ready events
        if (jQuery.fn.trigger) {
            jQuery(document).trigger("ready").off("ready");
        }
    }
},

看来,由于处理程序的异步循环,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 a ready() call.

Just make sure that your main ready() call comes first.

$(document).ready(function() {
    // Your normal ready handler
});

$(window).load(function() {
    // Placing code in another .ready() call in here will add it to the
    //    end of internal Array of ready handlers if any are pending
    $(document).ready(function() {
        // my onload code
    });
});

So if the ready() has already fired by the time the onload fires, your onload 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 the onload happens, that will mean that your original ready() 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.


ready: function (wait) {
    // Either a released hold or an DOMready/load event and not yet ready
    if ((wait === true && !--jQuery.readyWait) || (wait !== true && !jQuery.isReady)) {

        // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
        if (!document.body) {
            return setTimeout(jQuery.ready, 1);
        }

        // Remember that the DOM is ready
        jQuery.isReady = true;

        // If a normal DOM Ready event fired, decrement, and wait if need be
        if (wait !== true && --jQuery.readyWait > 0) {
            return;
        }

        // If there are functions bound, to execute
        readyList.fireWith(document, [jQuery]);

        // Trigger any bound ready events
        if (jQuery.fn.trigger) {
            jQuery(document).trigger("ready").off("ready");
        }
    }
},

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 the onload as I described above should remedy this.

憧憬巴黎街头的黎明 2025-01-11 09:53:19

是的。

  • 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.
束缚m 2025-01-11 09:53:19

不。亲自尝试一下:创建一个大型 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.

enter image description here

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).

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