coderef

beforeunloadLimited availability

The beforeunload event is fired when the current window is about to be unloaded. Typically this is used to display a dialog to confirm if users really want to leave the page when there is unsaved data that would be lost.

Not yet supported in all major browsers — check the table and provide a fallback.

Browser support

DesktopChrome
1+Supportedsince Dec 11, 2008
Current: 155
Edge
12+Supportedsince Jul 29, 2015
Current: 154
Firefox
1+Supportedsince Nov 9, 2004
Current: 157
Safari
1–2Not supported
3+Supportedsince Oct 26, 2007
Current: 27
MobileChrome Android
18+Supportedsince Jun 27, 2012
Current: 155
Firefox Android
4+Supportedsince Mar 29, 2011
Current: 157
Safari iOS
✕Not supportedup to 27
Current: 27

How to use

Not supported in Safari iOS. Use feature detection below and provide a fallback (or a polyfill) for those browsers.

Example

Full syntax and examples are on MDN: BeforeUnloadEvent.

Feature detection

JS · check before using
if ("BeforeUnloadEvent" in globalThis) {
  // BeforeUnloadEvent is available
} else {
  // load a polyfill or use a fallback
}

Sub-features

Support for each part of the feature (4). The overall table above shows when the whole feature works.

Sub-featureChromeEdgeFirefoxSafariChrome AndroidFirefox AndroidSafari iOS
BeforeUnloadEvent30121.573047
BeforeUnloadEvent.user_interaction60794411604411
Window.beforeunload_event11213184✕
Window.beforeunload_event.preventdefault_activation1191196111196✕