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

Properties

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).

Example

window.addEventListener("beforeunload", function (event) {
  // Most browsers.
  event.preventDefault();

  // Chrome/Chromium based browsers still need this one.
  event.returnValue = "\o/";
});

Notes

Since 25 May 2011, the HTML5 specification states that calls to window.alert(), window.confirm(), and window.prompt() methods may be ignored during this event. See the HTML5 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.

Specifications

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

Browser compatibility

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!

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

See also