The beforeunload
event is fired when the window, the document and its resources are about to be unloaded. The document is still visible and the event is still cancelable at this point.
If a string is assigned to the returnValue
Event property, a dialog appears asking the user for confirmation to leave the page (see example below). Some browsers display the returned string in the dialog box, but others display their own message. If no value is provided, the event is processed silently.
Note: To combat unwanted pop-ups, browsers may not display prompts created in beforeunload
event handlers unless the page has been interacted with, or may even not display them at all.
Attaching an event handler/listener to window
or document
's beforeunload
event prevents browsers from using in-memory page navigation caches, like Firefox's Back-Forward cache or WebKit's Page Cache.
Bubbles | No |
Cancelable | Yes |
Target objects | defaultView |
Interface | Event |
Property | Type | Description |
---|---|---|
target Read only
| EventTarget | The event target (the topmost target in the DOM tree). |
type Read only
| DOMString | The type of event. |
bubbles Read only
| Boolean | Does the event normally bubble? |
cancelable Read only
| Boolean | Is it possible to cancel the event? |
returnValue | DOMString | The current return value of the event (the message to show the user). |
The HTML specification states that authors should use the Event.preventDefault()
method instead of using Event.returnValue
. However, this is not yet supported by all browsers.
window.addEventListener("beforeunload", function (event) { // Cancel the event as stated by the standard. event.preventDefault(); // Chrome requires returnValue to be set. event.returnValue = ''; });
The HTML specification states that calls to window.alert()
, window.confirm()
, and window.prompt()
methods may be ignored during this event. See the HTML specification for more details.
Various browsers ignore the result of the event and do not ask the user for confirmation at all. The document will always be unloaded automatically. Firefox has a switch named dom.disable_beforeunload
in about:config to enable this behaviour.
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'beforeunload' in that specification. | Living Standard | |
HTML5 The definition of 'beforeunload' in that specification. | Recommendation | Initial definition |
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|---|
Activation using return "string";
| 1.0 | (Yes) | 1 | 4 | 12 | 3 |
Activation using event.returnValue = "string";
| 30.0 | (Yes) | (Yes) | (Yes) | ||
Activation using event.preventDefault()
| No support | (Yes) | (Yes) | 9 | 11 | |
Custom text support removed | 51.0 | No support | 44.0 (44.0) | No support | 38 | 9.1 |
Feature | Android | Android Webview | Edge | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|---|
Basic support | ? | (Yes) | (Yes) | ? | ? | ? | (no) defect | (Yes) |
Custom text support removed | ? | 51.0 | No support | 44.0 (44.0) | 51.0 |
DOMContentLoaded
readystatechange
load
unload
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload