You are here: Reference > JavaScript > client-side > event handling > events > onunload

onunload event | unload event

Browser support:
Occurs before the browser unloads the document.
The onunload event is not cancelable, because of security reasons. To display a comfirmation dialog box, where the user can confirm whether he wants to stay or leave the current page, use the onbeforeunload event.

How to register:

In HTML:
<ELEMENT onunload="handler">

In JavaScript:
object.onunload = handler;
object.addEventListener ("unload", handler, useCapture);
9
object.attachEvent ("onunload", handler);
You can find the related objects in the Supported by objects section below.
The event object is accessible to all event handlers in all browsers. The properties of the event object contain additional information about the current event. To get further details about these properties and the possible event handler registration methods, please see the page for the event object.
For a complete list of events, see the page for Events in JavaScript.

Basic information:

Bubbles No
Cancelable No
Event object Event

Actions that invoke the onunload event:

  • Navigating to another page directly in the browser or with a link.
  • Closing the current browser window or tab page.
  • Reloading the current page.
  • Manipulating the URL of the currently loaded page through the location object from JavaScript.
  • Invoking the window.navigate method.
  • Invoking the window.open or the document.open method to open a document in the same window.

The order of events related to the onunload event:

Action Event order
Any action that invokes the onunload event.
  1. onbeforeunload
  2. onunload

Example HTML code 1:

This example illustrates the use of the onunload event:
<head>
    <script type="text/javascript">
        function OnUnLoad () {
            alert ("The current document will be unloaded!");
        }
    </script>
</head>
<body onunload="OnUnLoad ()">
    <b>Close this window or press F5 to reload the page.</b>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content