Syntax
Use the event name in methods like addEventListener(), or set an event handler property.
js
addEventListener("beforeunload", (event) => { })
onbeforeunload = (event) => { }
Event type
A BeforeUnloadEvent. Inherits from Event.
Usage notes
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:
- Require sticky activation for the dialog to be displayed. In other words, the browser will only show the dialog box if the frame or any embedded frame receives a user gesture or user interaction. If the user has never interacted with the page, then there is no user data to save, so no legitimate use case for the dialog.
- Only show a generic browser-specified string in the displayed dialog. This cannot be controlled by the webpage code.
The beforeunload event suffers from some problems:
-
It is not reliably fired, especially on mobile platforms. For example, the
beforeunloadevent is not fired at all in the following scenario:- A mobile user visits your page.
- The user then switches to a different app.
- Later, the user closes the browser from the app manager.
-
In Firefox,
beforeunloadis not compatible with the back/forward cache (bfcache): that is, Firefox will not place pages in the bfcache if they havebeforeunloadlisteners, 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.
Event handler aliases
In addition to the Window interface, the event handler property onbeforeunload is also available on the following targets:
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 — 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.
js
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:
Specifications
| Specification |
|---|
| HTML # event-beforeunload |
| HTML # handler-window-onbeforeunload |
Browser compatibility
See also
BeforeUnloadEventinterface- Related events:
- Page Lifecycle API provides more useful guidance on handling page lifecycle behavior in your web apps.