The unload
event is fired when the document or a child resource is being unloaded.
Bubbles | No |
---|---|
Cancelable | No |
Interface | Event |
Event handler property | onunload |
It is fired after:
beforeunload
(cancelable event)pagehide
The document is in the following state:
- All the resources still exist (img, iframe etc.)
- Nothing is visible anymore to the end user
- UI interactions are ineffective (
window.open
,alert
,confirm
, etc.) - An error won't stop the unloading workflow
Please note that the unload event also follows the document tree: parent frame unload will happen before child frame unload
(see example below).
Examples
<!DOCTYPE html> <html> <head> <title>Parent Frame</title> <script> window.addEventListener('beforeunload', function(event) { console.log('I am the 1st one.'); }); window.addEventListener('unload', function(event) { console.log('I am the 3rd one.'); }); </script> </head> <body> <iframe src="child-frame.html"></iframe> </body> </html>
Below, the content of child-frame.html
:
<!DOCTYPE html> <html> <head> <title>Child Frame</title> <script> window.addEventListener('beforeunload', function(event) { console.log('I am the 2nd one.'); }); window.addEventListener('unload', function(event) { console.log('I am the 4th and last oneβ¦'); }); </script> </head> <body> β» </body> </html>
When the parent frame is unloaded, events will be fired in the order described by the console.log()
messages.
Specifications
Specification | Status | Comment |
---|---|---|
UI Events The definition of 'unload' in that specification. |
Working Draft |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
unload event | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 4 | Opera Full support 4 | Safari Full support 3 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 |
Legend
- Full support
- Full support
See also
- Related events:
DOMContentLoaded
,readystatechange
,load
- Unloading Documents — unload a document