The unload event is fired when the document or a child resource is being unloaded.
It is fired after:
- beforeunload (cancellable event)
- pagehide
The document is in a particular 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).
| Bubbles | No | 
| Cancellable | No | 
| Target objects | defaultView, Document, Element | 
| Interface | UIEventif generated from a user interface,Event | 
| Default action | None | 
Properties
| Property | Type | Description | 
|---|---|---|
| targetRead only | EventTarget | The event target (the topmost target in the DOM tree). | 
| typeRead only | DOMString | The type of event. | 
| bubblesRead only | Boolean | Whether the event normally bubbles or not | 
| cancelableRead only | Boolean | Whether the event is cancellable or not? | 
| viewRead only | WindowProxy | document.defaultView(windowof the document) | 
| detailRead only | long(float) | 0. | 
Example
<!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 console.log messages.
Related Events
Specifications
Document Tags and Contributors
                       Contributors to this page: 
        bunnybooboo, 
        dunxtand, 
        mfluehr, 
        danwaz, 
        ryanmurakami, 
        teoli, 
        Sheppy, 
        ethertank, 
        oncletom, 
        louisremi
                    
                    
                       Last updated by:
                      bunnybooboo,