aspxtreme

onbeforeunload Event

DHTML Events


Fires prior to a page being unloaded.

Syntax


Inline HTML <element onbeforeunload = "handler" ... > All platforms
Event property object.onbeforeunload = handler JScript® (compatible with ECMA-262 language specification) only
Named script <script FOR=object EVENT=onbeforeunload> Internet Explorer® only

Remarks


Bubbles No
Cancels No
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.

This event gives the programmer a chance to request that the user not leave the page. When a string is returned to this event, a dialog box displays that gives the user the option of staying on the page. The returned string displays in a predefined area.

<script language="JavaScript">
function Window_beforeUnload()  {
  alert("Window is unloading");
  event.returnValue = 
    "Click cancel to stay on this page.";
}
</script>

Event Object Properties

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

Event Object Properties

altKey Retrieves the current state of the ALT key.
clientX Retrieves the x-coordinate of the position of the cursor when the mouse is clicked, relative to the size of the client area of the window but excluding window decorations or scroll bars.
clientX Returns the y-coordinate of the position of the cursor when the mouse is clicked, relative to the size of the client area of the window but excluding window decorations or scroll bars.
ctrlKey Retrieves the state of the CTRL key.
returnValue Sets or retrieves the return value from the event.
shiftKey Retrieves the state of the SHIFT key.
type Retrieves the event name from the event object.

Example

The following example shows use of the onbeforeunload event.

Sample Code


Applies To

FRAMESET, window

See Also

onload, onunload



Books and more ...

Contents
HTML Elements / DOM Objects
HTML Element Attributes
CSS Attributes
DOM Object Properties
DOM-compliant Events Reference
DOM Methods Reference
DOM Collections Reference
 


Suggested Reading



Previous page Back to top Next page

Check out related books at Amazon

© 2000-2008 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support aspxtreme