onbeforeunload event
Examples
Execute JavaScript Upon leaving the current page (refresh or close):
try it"
Definition and Usage
onbeforeunload event is triggered when leaving the current page (refresh or shut down).
This event can be used for pop-up dialog box that prompts the user to continue browsing the page or leave the current page.
The default dialog box message is different depending on the browser, the standard message similar to "Do you want to leave this page?." This information can not be deleted.
But you can customize some news tips and standard information displayed in the dialog together.
Note: If you do not specify onbeforeunload event on the <body> element, you'll need to add an event on the window object, and create custom information (see the following syntax examples) using the returnValue property.
Note: In the Firefox browser, only the default reminder message (do not display the custom message).
Browser Support
Figures in the table represent the first browser to support this version of events.
event | |||||
---|---|---|---|---|---|
onbeforeunload | Yes | Yes | Yes | Yes | 15.0 |
grammar
In HTML:
In JavaScript:
JavaScript, use the addEventListener () method:
Note: Internet Explorer 8 and earlier versions of IE do not support addEventListener () method.
technical details
Whether to support the bubble: | No |
---|---|
It can be canceled: | Yes |
Event Type: | Event |
Supported HTML tags: | <Body> |
Event objects