Web/API/WindowEventHandlers/onunload

From Get docs


The onunload property of the WindowEventHandlers mixin is the EventHandler for processing unload events. These events fire when the window is unloading its content and resources. The resource removal is processed after the unload event occurs.

Note: Browsers equipped with pop-up blockers will ignore all Window.open() method calls in onunload event handler functions.


The onunload (and the unload event itself) are not the right features to use with sendBeacon(). Instead for sendBeacon(), use the visibilitychange and pagehide events. See discussion comments in the blog post Beacon API is broken.


Syntax

window.addEventListener("unload", function(event) { ... });
window.onunload = function(event) { ... };

Typically, it is better to use window.addEventListener() and the unload event, instead of onunload.

Specifications

Specification Status Comment
HTML Living StandardThe definition of 'onunload' in that specification. Living Standard
HTML 5.1The definition of 'GlobalEventHandlers' in that specification. Recommendation
HTML5The definition of 'GlobalEventHandlers' in that specification. Recommendation

Browser compatibility

Update compatibility data on GitHub

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
onunload Chrome

Full support Yes

Edge

Full support 12

Firefox

Full support Yes

IE

Full support Yes

Opera

Full support Yes

Safari

Full support Yes

WebView Android

Full support Yes

Chrome Android

Full support Yes

Firefox Android

Full support Yes

Opera Android

Full support Yes

Safari iOS

Full support Yes

Samsung Internet Android

Full support Yes

Legend

Full support  
Full support


In Firefox 1.5, using this event handler in your page prevents the browser from caching the page in the in-memory bfcache. See Using Firefox 1.5 caching for details.