The onbeforeunload attribute handles the window beforeunload event. It runs when the document is about to unload — close, reload, or navigate away — while the page is still visible.
Put it on <body>, or use window.addEventListener("beforeunload", …). Warn only about unsaved changes, then remove the listener when nothing is dirty.
Limited availability.
MDN: not Baseline across all major browsers. Support and dialog behavior vary; do not rely on it alone to save data.
Inline handlers discouraged.
Prefer addEventListener. Call event.preventDefault() (and set event.returnValue for legacy). Browsers show a generic message — custom strings are ignored.
Syntax
HTML Syntax
syntax.html
<body onbeforeunload="script">
script runs when beforeunload fires. Preferred: window.addEventListener("beforeunload", handler) with preventDefault().
Enable a leave warning for this tab, then try reload or close. Disable when done so you can leave this tutorial freely.
warning
Requires a prior click on this page (sticky activation). Dialog text is browser-controlled.
Demo
Leave warning is off.
Code
HTML Examples
Short study snippets. Use Try It Yourself, then reload that editor tab. Prefer listeners and dirty-only attachment in real apps.
Example 1 — Inline onbeforeunload on body
Recognize the attribute form. Returning a string is a legacy trigger for the property/attribute style.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>onbeforeunload demo</title>
</head>
<body onbeforeunload="return 'You have unsaved changes.';">
<p>Reload or close this tab to see the browser leave dialog.</p>
</body>
</html>
Named handler so you can remove the same function later.
On input — non-empty value adds beforeunload.
Empty again — remove the listener so exit is quiet.
Best practice — warn only when there is real data to lose.
Edge Cases & Pitfalls
Browsers limit this event to stop spammy leave prompts.
message
No custom dialog text
Modern browsers show a generic string. Your return value does not appear as the prompt.
activation
Needs user interaction
Sticky activation: if the user never interacted with the page, the dialog usually will not show.
mobile
Not reliable on mobile
Closing via the app switcher may skip beforeunload. Save important state earlier — do not depend on this event alone.
attach
Attach only when dirty
Permanent listeners annoy users. Add when unsaved; remove after save or clear.
style
Prefer listeners
Inline on* attributes mix concerns. Use addEventListener with preventDefault() in production.
Compatibility
Browser Support
onbeforeunload is a window event handler for beforeunload. Dialog rules and reliability vary by browser and platform.
✓ Limited availability
onbeforeunload
Works in major desktop browsers with restrictions (generic dialog, sticky activation). Mobile unload paths are less reliable. Not marked Baseline by MDN.
VariesSupport & behavior
Google ChromeModern
Supported*
Mozilla FirefoxModern
Supported*
Apple SafariModern
Supported*
Microsoft EdgeModern
Supported*
OperaModern
Supported*
Samsung InternetModern
Partial*
Internet ExplorerLegacy*
Supported*
beforeunloadLimited / restricted
Bottom line: *Dialogs are generic and often require prior user interaction. Prefer dirty-only listeners; save state without relying only on beforeunload.
Remember
Key Takeaways
Role: optional leave/reload confirmation for unsaved work.
Trigger:preventDefault() (+ returnValue legacy).
Prefer: dirty-only addEventListener, not always-on.
Limits: generic message; weak on some mobile exits.
One line:onbeforeunload can warn before leave — attach only when dirty, expect a generic dialog, and save data elsewhere too.
Frequently Asked Questions
It is an event handler for the beforeunload event. That event fires when the window or document is about to unload (close, reload, or navigate away). Sites use it to show a browser confirmation when there may be unsaved data.
It is a window-level handler. In HTML, put it on body (window handlers on body listen on window), or use window.addEventListener("beforeunload", …) / window.onbeforeunload.
No. Modern browsers show a generic dialog. Custom return strings are ignored for the visible text.
Best practice: call event.preventDefault() and also set event.returnValue for older browsers. Returning a string only works with the onbeforeunload property, not addEventListener.
Only while there are unsaved changes. Add the listener when the page becomes dirty; remove it when data is saved or cleared. MDN also notes sticky activation: the user must have interacted with the page.
Not always. Closing the browser from the app switcher may skip beforeunload. Prefer saving important state earlier (for example on visibility changes) instead of relying only on this event.