onbeforeunload Event

DHTML, HTML, & CSS

onbeforeunload Event


Fires prior to a page being unloaded.

Syntax

Inline HTML<ELEMENT onbeforeunload = "handler" ... >All platforms
Event propertyobject.onbeforeunload = handlerJScript (compatible with ECMA 262 language specification) only
Named script<SCRIPT FOR = object EVENT = onbeforeunload>Internet Explorer only

Remarks

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_submit control, or invoke the form submit method.
Default action Signals that the page is about to be unloaded.

When a string is returned to this event, a dialog box displays that gives the user the option of staying on the page.

Event Object Properties

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

Event Object Properties

altKeyRetrieves the current state of the ALT key.
clientXRetrieves the x-coordinate of the mouse cursor relative to the client area of the window, excluding window decorations or scroll bars.
clientYRetrieves the y-coordinate of the mouse cursor relative to the client area of the window, excluding window decorations or scroll bars.
ctrlKeyRetrieves the state of the CTRL key.
returnValueSets or retrieves the return value from the event.
shiftKeyRetrieves the state of the SHIFT key.
typeRetrieves the event name from the event object.

Example

This example uses the onbeforeunload event to prompt the user to remain on the page.

Sample Code

<HTML>
<HEAD>
<SCRIPT>
function closeIt()
  {
    event.returnValue = "Any string value here will force a dialog 
                         box to appear before closing the window.";
  }
</SCRIPT>
</HEAD>
<BODY onbeforeunload="closeIt()">
<P>Navigate to another page to fire the before unload event.
</BODY>
</HTML>

Applies To

[ Object Name ]
PlatformVersion
Win16:
Win32:
Mac:
Unix:
WinCE:
Version data is listed when the mouse hovers over a link, or the link has focus.
FRAMESET, window

See Also

data bindingInternet Link, onload, onunload


Back to topBack to top

Did you find this topic useful? Suggestions for other topics? write us!Internet Link

© 1999 microsoft corporation. all rights reserved. terms of useInternet Link.