window.onload 相当于 Ajax 应用程序吗?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
简短的回答是(目前)没有通用的方法来解决这个问题。
当 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.
我已经与这个问题斗争过几次了,我通常需要某种 onload 事件触发的原因是能够与 HTML DOM 交互,并让 getElementById、getElementsByTagName 或各种其他 DOM 选择器返回一些内容,而不是不返回任何内容。
再说一遍,我不确定您要解决的确切问题,但是如果您必须使用某种onload,并且这是因为某种DOM遍历,那么您可以稍微作弊使用以下代码:
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:
您可以通过全局监听
DOMSubtreeModified
和readystatechange
事件来监听更改和请求,并使用这些事件而不是load
事件无论你想做什么。You might be able to listen for changes and requests by globally listening for
DOMSubtreeModified
andreadystatechange
events, and use those events instead of theload
event for whatever you are trying to do.我建议使用 jQuery 的 $(...).ready() 方法。
I'd recomend using jQuerys $(...).ready() method.
除了 Rasmus Kaj 的回答之外,如果您使用的是 jQuery,我会引导您查看 全局 Ajax 事件处理程序。
jQuery.fn.ajaxComplete
(http://api.jquery.com/ajaxcomplete/ )jQuery.fn.ajaxStop
(http://api.jquery .com/ajaxstop/)请注意,这与本机 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.
jQuery.fn.ajaxComplete
(http://api.jquery.com/ajaxcomplete/)jQuery.fn.ajaxStop
(http://api.jquery.com/ajaxstop/)Note, that this has nothing to do with the native onload event.