Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
HTML and CSS
Events
 onunload Event

  Switch on low bandwidth view
onunload Event

Fires immediately before the object is unloaded.

Syntax

Inline HTML<ELEMENT onunload = "handler" ... > All platforms
Event propertyobject.onunload = handlerJScript only
object.onunload = GetRef("handler")Visual Basic Scripting Edition (VBScript) 5.0 or later only
Named script <SCRIPT FOR = object EVENT = onunload> Internet Explorer only

Event Information

BubblesNo
CancelsNo
To invoke
  • Close the current browser window.
  • Navigate to another location by entering a new address or selecting a Favorite.
  • Click the Back, Forward, Refresh, or Home button.
  • Click on an anchor that refers the browser to another Web page.
  • Invoke the anchor.click method.
  • Invoke the document.write method.
  • Invoke the document.open method.
  • Invoke the document.close method.
  • Invoke the window.close method.
  • Invoke the window.open method, providing the possible value _self for the window name.
  • Invoke the window.navigate or NavigateAndFind method.
  • Invoke the location.replace method.
  • Invoke the location.reload method.
  • Specify a new value for the location.href property.
  • Submit a form to the address specified in the ACTION attribute via the INPUT type=submit control, or invoke the form.submit method.
Default action Removes the object or document from the browser window.

Event Object Properties

Although event handlers in the DHTML Object Model do not receive parameters directly, a handler can query the event object for the following event properties.

Available Properties

altKey Retrieves a value that indicates the state of the ALT key.
altLeft Retrieves a value that indicates the state of the left ALT key.
ctrlLeft Sets or retrieves the state of the left CTRL key.
shiftLeft Retrieves the state of the left SHIFT key.
type Sets or retrieves the event name from the event object.

Remarks

If you call window.open from this event, the Pop-up Blocker feature in Microsoft Internet Explorer 6 prevents the pop-up window from appearing.

Example

This example shows how to use the onunload event to run script when the window object has been unloaded.

<head>
<script type="text/javascript" for="window" event="onunload">
    alert("The onunload event fired for the window object.");
</script>
<script type="text/javascript">
    function fnRelocate()
    {
    location.href="/workshop/samples/author/dhtml/refs/onunloadEX_target.htm";
    }
</script>
</head>

<body>

<input type="button" value="Go To Page 2" onclick="fnRelocate()">

</body>
This feature requires Microsoft Internet Explorer 4.0 or later. Click the following icon to install the latest version. Then reload this page to view the sample.

Standards Information

This event is defined in HTML 4.0 World Wide Web link.

Applies To

BODY, FRAMESET, window, HTMLBodyElement Constructor, HTMLFrameSetElement Constructor, Window Constructor

See Also

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker