W3cubDocs

/Web APIs

BeforeUnloadEvent

The BeforeUnloadEvent interface represents the event object for the beforeunload event, which is fired when the current window, contained document, and associated resources are about to be unloaded.

See the beforeunload event reference for detailed guidance on using this event.

Event BeforeUnloadEvent

Instance properties

Inherits properties from its parent, Event.

returnValue Deprecated

When set to a truthy value, triggers a browser-controlled confirmation dialog asking users to confirm if they want to leave the page when they try to close or reload it. This is a legacy feature, and best practice is to trigger the dialog by invoking event.preventDefault() instead, as shown in the example.

Instance methods

Inherits methods from its parent, Event.

Examples

In the following example we have an HTML text <input> to represent some data that could be changed and require saving:

html

<form>
  <input type="text" name="name" id="name" />
</form>

Our JavaScript attaches an input event listener to the <input> element that listens for changes in the inputted value. When the value is updated to a non-empty value, a beforeunload event listener is attached to the Window object.

If the value becomes an empty string again (i.e. the value is deleted), the beforeunload event listener is removed again. It would be more efficient to just add the beforeunload listener once, and conditionally call preventDefault() based on the value of the target input. Best practice however is to add the listener when needed (i.e. when there is unsaved data that could be lost) and remove it again when it is not needed, for reasons outlined in the beforeunload event usage notes.

The beforeunload event handler function invokes preventDefault() to trigger the warning dialog when the user closes or navigates the tab.

js

const beforeUnloadHandler = (event) => {
  event.preventDefault();
};

const nameInput = document.querySelector("#name");

nameInput.addEventListener("input", (event) => {
  if (event.target.value !== "") {
    window.addEventListener("beforeunload", beforeUnloadHandler);
  } else {
    window.removeEventListener("beforeunload", beforeUnloadHandler);
  }
});

When the <input> value is non-empty, if you try to close, navigate, or reload the page the browser displays the warning dialog. Try it out:

Note: The browser-generated confirmation dialog can also be triggered by setting the event.returnValue property to a truthy value on the beforeunload event object, or by returning a truthy value from the event handler function. However, these are legacy mechanisms, and best practice is to trigger the dialog by invoking event.preventDefault() as shown in the example above.

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
BeforeUnloadEvent 30 79 1.5 4 17 7 ≤37 30 4 18 7 3.0
returnValue 119
30–119Before Chrome 119, an empty string incorrectly activated the confirmation dialog.
119
79–119Before Edge 119, an empty string incorrectly activated the confirmation dialog.
1.5 9
17Before Opera false, an empty string incorrectly activated the confirmation dialog.
7 ≤37 119
30–119Before Chrome 119, an empty string incorrectly activated the confirmation dialog.
4
18Before Opera false, an empty string incorrectly activated the confirmation dialog.
7 3.0
user_interaction 60 79 44 No 47 11 60 60 44 44 11 8.0

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/BeforeUnloadEvent