JavaScript onunload 仅在文档​​完全加载时调用?

发布于 2024-08-20 06:50:40 字数 853 浏览 9 评论 0原文

我还没有看到 David Flanagan 的《JavaScript - the Definitive Guide》一书(我也在 O'Reilly Answers 上问过这个问题)或其他任何地方提到过这一点,但我发现 window.onunload除非文档完全加载,否则不会调用,这很烦人!对于 IE8 和 Chrome 3 来说确实如此,至少

我有一段 Javascript 我想添加到网页底部。如果用户碰巧在页面加载之前单击关闭页面(^ 见下文),并且在我的 Javascript 有机会运行之前,我希望将其捕获在 onunload 中。但这是不可能的,因为 onunload 事件处理程序仅在文档完全加载时才会被调用。如果我无法使用 onunload,是否可以使用其他方法来实现相同的目的?

^ “Javascript - 权威指南”建议在页面加载之前用户无法与页面进行交互。我相信这是错误的。书中是这么说的:

“Javascript执行模型中的另一个灰色区域是在文档完全加载之前是否可以调用事件处理程序的问题。到目前为止,我们对Javascript执行模型的讨论得出的结论是所有事件处理程序总是在所有脚本执行后触发。虽然这种情况通常会发生,但任何标准都不需要这样做。如果文档很长或通过慢速网络连接加载,则浏览器可能会部分呈现文档并允许。用户在所有脚本和 onload 处理程序运行之前开始与其交互(并触发事件处理程序),如果此类事件处理程序调用尚未定义的函数,它将失败(这是在脚本中定义所有函数的原因之一)。如果这样的事件处理程序尝试操作尚未解析的文档部分,则这种情况在实践中并不常见,并且通常不值得进行额外的编码工作。积极防范它。” (第 5 版,第 256 页)

我在 IE8 上进行了测试,结果表明可以在页面加载的任何阶段与页面交互(并通过锚链接离开)。

感谢您的帮助, 保罗

I haven't seen any mention of this is David Flanagan's "JavaScript - the Definitive Guide" book (I've asked this question on O'Reilly Answers too), or anywhere else for that matter, but I am finding that window.onunload is not invoked unless the document fully loaded, which is quite annoying! That's true for IE8 and Chrome 3 at least

I have a piece of Javascript I want to add to the bottom of web pages. If the user happens to click off the page before the page has loaded(^ see below), and before my Javascript has had a chance to run, I was hoping to trap that in the onunload. But that's not possible as the onunload event handler is only invoked when the document fully loads. If I can't use onunload, is there anything else I can use to achieve the same?

^ "Javascript - The Definitive Guide" suggests user interaction with the page cannot happen until the page is loaded. I believe this is wrong. Here is what the book says:

"Another grey area in the Javascript execution model is the question of whether event handlers can be invoked before the document is fully loaded. Our discussion of the Javascript execution model has so far concluded that all event handlers are always triggered after all scripts have been executed. While this typically happens, it is not required by any standard. If a document is very long or is being loaded over a slow network connection, the browser might partially render the document and allow the user to begin interacting with it (and triggering event handlers) before all scripts and onload handlers have run. If such an event handler invokes a function that is not yet defined, it will fail. (this is one reason to define all functions in scripts in the of a document.) And if such an event handler attempts to manipulate a part of the document that has not yet been parsed, it will fail. This scenario is uncommon in practice, and it is not usually worth the extra coding effort required to aggressively protect against it."
(5th Edition, Page 256)

I performed tests on IE8 that showed it was possible to interact with the page (and leave it via an anchor link) at any stage in the loading of the page.

Thanks for any help,
Paul

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

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

发布评论

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

评论(1

檐上三寸雪 2024-08-27 06:50:40

对我来说,在解析文档之前 onunload 事件无法触发是有道理的。文档解析和脚本执行通常在同一个线程上运行,如果用户要离开页面,则在触发 onunload 事件后线程将被取消。由于解析是在用户导航离开页面时使用线程,因此浏览器将具有以下选项:

  • 完成解析并触发卸载事件 - 这会延迟导航到下一页,因此这实际上并不是一个可行的选择。
  • 取消解析并触发卸载事件 - 这是一个更可行的选项,但您可能会看到一些 UA 不打扰。
  • 只需停止线程并导航到下一页 - 这似乎就是 IE 和 Chrome 中发生的情况

也许您可以看到 onbeforeunload 事件被触发?但我认为这不会有什么不同。我认为最好的办法是寻找优化页面以加快加载速度的方法。

It makes sense to me that an onunload event could not fire until the document is parsed. Document parsing and script execution generally run on the same thread and if a user was to navigate away from the page then the thread would be cancelled after the onunload event is fired. Since the parsing is using the thread at the time the user navigates away from the page, the browser would have the following options:

  • Complete the parsing and fire the unload event - which would delay navigation to the next page, so this isn't really a feasible option.
  • Cancel parsing and fire the unload event - this is a more feasible option but you could probably see some UAs not bothering.
  • Just stop the thread and navigate to the next page - which seems to be what's happening for you in IE and Chrome

Maybe you could see if the onbeforeunload event is fired? I don't think it will make a difference though. I think the best thing to do would be to look at ways you can optimize the page to load faster.

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