简体   繁体   中英

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.

In the latest version of Chrome, my onunload anonymous function seems unreachable.

I'm following MDN Web Docs ( 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 . Exiting out of the tab, or completely closing the browser works as expected.

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

Instead unload, use window.onbeforeunload for Chrome and it will work fine!

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

Here is the event triggered in Chrome when I close the window. 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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