Latest web development tutorials

onbeforeunload event

Event Object Reference Event objects

Examples

Execute JavaScript Upon leaving the current page (refresh or close):

<Body onbeforeunload = "return myFunction ()">

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:

<Elementonbeforeunload = "myScript"> try

In JavaScript:

object .onbeforeunload = function () {myScript }; try

JavaScript, use the addEventListener () method:

object .addEventListener ( "beforeunload", myScript ); try

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 Object Reference Event objects