chrome 的onbeforeunload事件没有触发

onbeforeunload event is not working when user not click inside the body of page

I want call one function when user open any page and without click on that page, he just close the page.

I have tried this to put one button and click on that button using coding after page loading is complete , but it is not working.

I am follow this link. W3School

In this link , when you not click in result portion and reload page then it will not display alert , but when you click result portion and reload page then it will displaying a alert.

This is because the onbeforeunload is attached to the <iframe>'s window object, and when you do unload the main page, this event won't fire unless you had focus on this iframe.

Same will happen with StackSnippet here:

onbeforeunload = e => true;
<span>click to toggle iframe's focus</span>

原文地址:https://www.cnblogs.com/oxspirt/p/8625897.html