window.onload 相当于 Ajax 应用程序吗?

发布于 2024-08-11 00:13:44 字数 334 浏览 2 评论 0原文

我正在使用 window.onload 调用必须在页面之后执行的 JavaScript 代码已满载。据我所知,这是调用此类脚本的推荐方法。

但是,它不适用于某些 Ajax 站点,例如 www.bing.com实例 - window.onload 在页面完全呈现之前调用。

有什么建议吗?

I'm using window.onload to call my JavaScript code that has to be executed after the page is fully loaded. From what I read this is the recommended method to call such scripts.

However, it does not work for some Ajax sites, www.bing.com for instance - window.onload is called before the pages is fully rendered.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

朮生 2024-08-18 00:13:44

简短的回答是(目前)没有通用的方法来解决这个问题。

当 AJAX 发挥作用时,“页面”的定义是相当可替代的 - 很难区分旨在成为初始页面加载一部分的 AJAX 和可能不是的 AJAX。因此,浏览器自行决定何时应该触发 window.onload(),并且它并不总是在您想要的位置结束。

幸运的是,大多数人不需要通用的解决方案,而是特定的解决方案。如果您关心 bing.com,那么您可能可以了解 bing.com 的工作原理,并设计代码以在网站达到您认为可接受的状态时触发。

The short answer is that there's no general way to solve this problem (right now).

The definition of a "page" is pretty fungible when AJAX comes into play - it's pretty hard to tell the difference between AJAX that is intended to be part of the initial page load, and that which might not be. So, browsers are left on their own to determine when window.onload() should be fired, and it doesn't always end up where you want.

Luckily, most people don't need a general solution, but rather a specific one. If you care about bing.com, then you can probably look at how bing.com works and design your code to trigger when the site reaches a state that you find acceptable.

定格我的天空 2024-08-18 00:13:44

我已经与这个问题斗争过几次了,我通常需要某种 onload 事件触发的原因是能够与 HTML DOM 交互,并让 getElementById、getElementsByTagName 或各种其他 DOM 选择器返回一些内容,而不是不返回任何内容。

再说一遍,我不确定您要解决的确切问题,但是如果您必须使用某种onload,并且这是因为某种DOM遍历,那么您可以稍微作弊使用以下代码:

window.onload = pageChecker;

function pageChecker() {
    // Onload has fired, but we don't trust it.
    // Check to see if our deepest nested element can be found
    var testElement = document.getElementById("importantElement");

    if ( !testElement ) {
        // Try again in a bit, adjust timeout to what your users
        // can tolerate
        setTimeout(pageChecker, 50);
    }
    else {
        //
        // ... the element exists, run important code below ...
        //
    }
}

I've wrestled with this a couple of times, and my usual reason for needing some sort of onload event triggering is to be able to interact with the HTML DOM and have getElementById, getElementsByTagName, or various other DOM selectors, return something other than nothing.

Once again, I'm not sure of the exact problem you are trying to solve, but if you must use some sort of onload, and it's because of DOM traversal of some kind, you can cheat a bit with the following sort of code:

window.onload = pageChecker;

function pageChecker() {
    // Onload has fired, but we don't trust it.
    // Check to see if our deepest nested element can be found
    var testElement = document.getElementById("importantElement");

    if ( !testElement ) {
        // Try again in a bit, adjust timeout to what your users
        // can tolerate
        setTimeout(pageChecker, 50);
    }
    else {
        //
        // ... the element exists, run important code below ...
        //
    }
}
揽清风入怀 2024-08-18 00:13:44

您可以通过全局监听 DOMSubtreeModifiedreadystatechange 事件来监听更改和请求,并使用这些事件而不是 load 事件无论你想做什么。

You might be able to listen for changes and requests by globally listening for DOMSubtreeModified and readystatechange events, and use those events instead of the load event for whatever you are trying to do.

打小就很酷 2024-08-18 00:13:44

我建议使用 jQuery 的 $(...).ready() 方法。

I'd recomend using jQuerys $(...).ready() method.

青衫负雪 2024-08-18 00:13:44

除了 Rasmus Kaj 的回答之外,如果您使用的是 jQuery,我会引导您查看 全局 Ajax 事件处理程序

请注意,这与本机 onload 事件无关。

In addition to the answer from Rasmus Kaj and if you are using jQuery, I would direct you to take a look at Global Ajax Event Handlers.

Note, that this has nothing to do with the native onload event.

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