The onbeforeunload event is used like this:
Copy the code code as follows:
<script language="javascript">
g_blnCheckUnload = true;
function RunOnBeforeUnload() {
if (g_blnCheckUnload) {
window.event.returnValue = 'You will lose any unsaved content';
}
}
</script>
<body onbeforeunload="RunOnBeforeUnload()">
</body>
In this way, every time the browser window is closed or a new page is opened, the onbeforeunload event must be triggered.
The excerpt is as follows: onbeforeunload event:
Note: Firefox and IE among the three major browsers support the onbeforeunload event, but Opera does not yet support it.
usage:
object.onbeforeunload = handler
<element onbeforeunload = “handler” … ></element>
Description: When the event is triggered, a dialog box with confirmation and cancellation pops up. If you confirm, you will leave the page, if you cancel, you will continue to stay on this page. The handler can set a return value as the displayed text of the dialog box.
Triggered by:
Close browser window
When going to other pages through the address bar or favorites
When clicking return, forward, refresh, or homepage,
When clicking on a url link that goes to another page
When any of the following events is called: click, document write, document open, document close, window close, window navigate, window NavigateAndFind, location replace, location reload, form submit.
When using window open to open a page and pass the name of the window on this page to the page to be opened.
When reassigning the value of location.href.
When submitting a form with a specified action through the input type="submit" button.
Can be used on the following elements: BODY, FRAMESET, window
Platform support: IE4+/Win, Mozilla 1.7a+, Netscape 7.2+, Firefox0.9+