BeforeUnloadEvent: returnValue property
The returnValue
property of the BeforeUnloadEvent
interface, when set to a truthy value, triggers a browser-generated confirmation dialog asking 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.
Note: returnValue
is a legacy feature, and best practice is to trigger the dialog by invoking Event.preventDefault()
on the BeforeUnloadEvent
object instead. See the beforeunload
event reference for detailed up-to-date guidance.
Value
returnValue
is initialized to an empty string (""
) value.
Setting it to just about any truthy value will cause the dialog to be triggered on page close/reload, however note that it also requires sticky activation. In other words, the browser will only show the dialog 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.
Note: A generic browser-specified string is displayed in the dialog. This cannot be controlled by the webpage code.
Examples
See the beforeunload
event reference page for a best practice example.
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 |
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 |