Leave-page confirmation (beforeunload)
Shows a generic Leave site? dialog, only if the person interacted with the page.
In one line
Can show a generic Leave site? warning when you close or leave, after you have interacted
Shows a generic Leave site? dialog, only if the person interacted with the page.
A site can ask the person to confirm closing or leaving, often to protect unsaved work. Browsers hide the site's own text, so it cannot scare people with custom messages.
What it can and can't do
What the user does: You click, tap or type on the page, then try to close or leave it
It can
- Show a Leave site? box to protect your unsaved work
- Show it when you close the tab or window
- Show it when you press back or go to another site
It cannot
- Show custom text, since browsers hide the site's own message
- Show the box if you have not touched the page
- Block you for good, since you can still leave
- Work on iPhone browsers
How it differs between platforms: Works in Chrome, Edge, Firefox and Safari on computers. iPhone browsers do not show it.
Browser support
| Browser | Status | First version | Released | Note |
|---|---|---|---|---|
| Chrome | Supported | 1 | 2008-12-11 | None |
| Edge | Supported | 12 | 2015-07-29 | None |
| Firefox | Supported | 1 | 2004-11-09 | None |
| Safari | Supported | 3 | 2007-10-26 | None |
| Safari on iPhone | Not supported | Not available | Not listed | None |
| Chrome on Android | Supported | 18 | 2012-06-27 | None |
| Samsung Internet | Supported | 1.0 | 2013-04-27 | None |
How a site asks for it
| API | window.addEventListener('beforeunload', e => { e.preventDefault(); e.returnValue = ''; }) |
| Browser compat keys | api.Window.beforeunload_eventapi.BeforeUnloadEvent |
| How access is granted | gesture |
| What the user sees | No prompt (needs a click, tap or keypress) |
| Needs a secure context (HTTPS) | No |
| Needs a user gesture | Yes |
| Activation | sticky |
| How long it lasts | Fires on each unload attempt while the listener exists |
| How to take it back | Nothing to revoke (no permission is stored) |
| Inside an iframe | Dialog suppressed in cross-origin iframes without activation |
Typical flow
- Person interacts with the page (sticky activation)
- Page registers beforeunload and sets returnValue
- On close or navigate, browser shows generic confirmation
Data you get
Shape: none
No individual data fields. The signal is the access itself.
Technical limits
- Needs sticky user activation: no dialog if the page was never clicked or typed in (Chrome 60)
- Custom message text is ignored (Chrome 51, Firefox 44, Safari 9.1)
- Cannot run async work or block navigation indefinitely: dialog is the only block
- A beforeunload listener makes a page ineligible for the back/forward cache in Chrome desktop and Firefox
- Not reliable on mobile: tab discard and app switch skip it; use pagehide and visibilitychange
The story
What it is
A page can show a generic Are you sure you want to leave dialog, only after you used the page.
For you
If you try to close a page with unsaved work, a box asks Leave site? You can stay or leave. Pages you never touched cannot show it.
For makers
Makers use it to protect unsaved work in forms, editors and checkouts. Browsers use a fixed message to stop it being used to trap people. Misuse by scam sites is limited by the click rule.
Where you see it
| Type | Example | Apps |
|---|---|---|
| productivity | A web editor like Google Docs warns you if edits are not saved yet when you close the tab. | Google Docs |
| shopping | A checkout page asks if you want to leave with items in your cart and a half filled form. | None named |
| games | A browser game asks before you leave a ranked match where leaving counts as a loss. | None named |
| creative | A web design tool warns about unsynced changes. | Figma |
| education | A test platform warns a student before they close an exam. | None named |
| banking | A bank page warns you before you leave a half finished transfer. | None named |
| security | A scam page tries to trap you with repeated Leave prompts, but browsers allow it only after you click. | None named |
Risks
- Can trap users
- Annoying prompts
What the platform says
- MDN Web Docs: beforeunload event. Lets a page warn before you leave, with a standard message, only if you have interacted with it.
Good uses and abuse
Legitimate uses
- Unsaved form or document warning
Known abuse
- Retention dark pattern
- Exit-intent friction
Scores
| Score | Value | Meaning |
|---|---|---|
| Sensitivity | 1 of 5 | How revealing the data is |
| Utility | 4 of 5 | How useful it is to honest apps |
| Friction | 0 of 5 | How much the user has to do |
| Fingerprint bits | Not rated | Rough identifying entropy, when known |
History
- 2016-01, Firefox 44: Custom message removed
- 2016-05, Chrome 51: Custom beforeunload message removed
- 2017-07, Chrome 60: Dialog requires user interaction with the page
Related capabilities
Related
- Push history entries (back button limits). Can add extra Back button steps, but browsers skip steps made without a tap
- Beacons and keepalive requests. Can send data to a server as you leave or close the page
Sources
How well checked: Based on official documentation.