The onunload property of the WindowEventHandlers mixin is the event handler 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 method calls in onunload event handler functions.

Warning: 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.


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

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


HTML Standard (HTML)
# handler-window-onunload

Browser compatibility

BCD tables only load in the browser

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.