This feature is not Baseline because it does not work in some of the most widely-used browsers.
The beforeunload event is fired when the current window, contained document, and associated resources are about to be unloaded. The document is still visible and the event is still cancelable at this point.
The main use case for this event is to trigger a browser-generated confirmation dialog that asks users to confirm if they really want to leave the page when they try to close or reload it, or navigate somewhere else. This is intended to help prevent loss of unsaved data.
The dialog can be triggered in the following ways:
preventDefault() method.returnValue property to a non-empty string value or any other truthy value.return "string". Note that this only works when the function is attached via the onbeforeunload property, not the addEventListener() method. This behavior is consistent across modern versions of Firefox, Safari, and Chrome.The last two mechanisms are legacy features; best practice is to trigger the dialog by invoking preventDefault() on the event object, while also setting returnValue to support legacy cases.
Use the event name in methods like addEventListener(), or set an event handler property.
addEventListener("beforeunload", (event) => { })
onbeforeunload = (event) => { }
A BeforeUnloadEvent. Inherits from Event.
To trigger the dialog being shown when the user closes or navigates the tab, a beforeunload event handler function should call preventDefault() on the event object. You should note that modern implementations:
The beforeunload event suffers from some problems:
It is not reliably fired, especially on mobile platforms. For example, the beforeunload event is not fired at all in the following scenario:
Note: It is recommended to use the visibilitychange event as a more reliable signal for automatic app state saving that gets around problems like the above. See Don't lose user and app state, use Page Visibility for more details.
In Firefox, beforeunload is not compatible with the back/forward cache (bfcache): that is, Firefox will not place pages in the bfcache if they have beforeunload listeners, and this is bad for performance.
It is therefore recommended that developers listen for beforeunload only when users have unsaved changes so that the dialog mentioned above can be used to warn them about impending data loss, and remove the listener again when it is not needed. Listening for beforeunload sparingly can minimize the effect on performance.
In addition to the Window interface, the event handler property onbeforeunload is also available on the following targets:
In the following example we have an HTML text <input> to represent some data that could be changed and require saving:
<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 — as mentioned above in the Usage notes, the listener should be removed when there is no unsaved data to warn about.
The beforeunload event handler function invokes event.preventDefault() to trigger the warning dialog when the user closes or navigates the tab. We have also included event.returnValue = true in the handler function so that any browsers that don't support the event.preventDefault() mechanism will still run the demo correctly.
const beforeUnloadHandler = (event) => {
// Recommended
event.preventDefault();
// Included for legacy support, e.g. Chrome/Edge < 119
event.returnValue = true;
};
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:
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
beforeunload_event |
1 | 12 | 1 | 12 | 3 | 18 | 4 | 12 | No | 1.0 | 4.4 | No |
event_returnvalue_activation |
11930–119Before Chrome 119, an empty string incorrectly activated the confirmation dialog. |
11979–119Before Edge 119, an empty string incorrectly activated the confirmation dialog. |
6 | 10517–105Before Opera 105, an empty string incorrectly activated the confirmation dialog. |
8 | 11930–119Before Chrome Android 119, an empty string incorrectly activated the confirmation dialog. |
6 | 7918–79Before Opera Android 79, an empty string incorrectly activated the confirmation dialog. |
No | 25.02.0–25.0Before Samsung Internet 25.0, an empty string incorrectly activated the confirmation dialog. |
1194.4–119Before WebView Android 119, an empty string incorrectly activated the confirmation dialog. |
No |
generic_string_displayed |
51 | 12 | 44 | 38 | 9.1 | 51 | 44 | 41 | No | 5.0 | 51 | No |
preventdefault_activation |
119 | 11912–79 | 6 | 105 | 11 | 119 | 6 | 79 | No | 25.0 | 119 | No |
return_string_activation |
1 | 12 | 1 | 12 | 3 | 18 | 4 | 12 | No | 1.0 | 4.4 | No |
BeforeUnloadEvent interface
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event