简体   繁体   English

如何获取窗口onunload事件触发器以在Chrome v73.x中工作?

[英]How do I get window onunload event trigger to work in Chrome v73.x?

It appears that with the latest release of Chrome (Version 73.0.3683.103 (Official Build) (64-bit)), window onunload event handler isn't being called. 似乎在最新版本的Chrome(版本73.0.3683.103(正式版本)(64位))中,未调用窗口onunload事件处理程序。

In the latest version of Chrome, my onunload anonymous function seems unreachable. 在最新版本的Chrome中,我的onunload匿名函数似乎无法访问。

I'm following MDN Web Docs ( https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onunload ). 我正在关注MDN Web文档( https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onunload )。

window.addEventListener('unload', function (e) {
  console.log('It worked');
  debugger;
});

When I run developer tools in each browser, my snippet of code in Chrome Version 66.0.3359.117, and in Firefox 66.x pauses on the debugger breakpoint as expected when refreshing the window or clicking the back/forward button . 当我在每个浏览器中运行开发人员工具时,在刷新窗口或单击“后退/前进”按钮时,我在Chrome版本66.0.3359.117和Firefox 66.x中的代码片段会按预期在调试器断点处暂停。 Exiting out of the tab, or completely closing the browser works as expected. 退出选项卡或完全关闭浏览器即可正常工作。

https://codepen.io/anon/pen/jRxJxJ https://codepen.io/anon/pen/jRxJxJ

Instead unload, use window.onbeforeunload for Chrome and it will work fine! 而是使用Chrome的window.onbeforeunload卸载,它将正常工作!

window.onbeforeunload = function (e) {
  console.log('It worked');
  debugger;
};

Here is the event triggered in Chrome when I close the window. 这是我关闭窗口时在Chrome中触发的事件。 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM