www.fgks.org   »   [go: up one dir, main page]

Click to Rate and Give Feedback
 onbeforeunload Event
onbeforeunload Event

Fires prior to a page being unloaded.

Syntax

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

Event Information

BubblesNo
CancelsYes
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 Signals that the page is about to be unloaded.

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 Sets or retrieves a value that indicates the state of the ALT key.
altLeft Sets or retrieves a value that indicates the state of the left ALT key.
clientX Sets or retrieves the x-coordinate of the mouse pointer's position relative to the client area of the window, excluding window decorations and scroll bars.
clientY Sets or retrieves the y-coordinate of the mouse pointer's position relative to the client area of the window, excluding window decorations and scroll bars.
ctrlKey Sets or retrieves the state of the CTRL key.
ctrlLeft Sets or retrieves the state of the left CTRL key.
returnValue Sets or retrieves the return value from the event.
shiftKey Sets or retrieves the state of the SHIFT key.
shiftLeft Retrieves the state of the left SHIFT key.
type Sets or retrieves the event name from the event object.

Remarks

When a string is assigned to the returnValue property of window.event, a dialog box appears that gives users the option to stay on the current page and retain the string that was assigned to it. The default statement that appears in the dialog box, "Are you sure you want to navigate away from this page? ... Press OK to continue, or Cancel to stay on the current page.", cannot be removed or altered.

Example

This example uses the onbeforeunload event to ask users whether they want to remain on the current page or navigate to a new URL. When the user clicks on the hyperlink or attempts to close the browser window, the onbeforeunload event fires on the body and a dialog box appears. If the user chooses 'OK', the page navigates to the new URL (www.microsoft.com) or closes the window; if the user chooses 'Cancel', the page remains the same.

<html>

<head>
<script type="text/javascript">
function closeIt()
  {
    event.returnValue = "Any string value here forces a dialog box to appear before closing the window.";
  }
</script>
</head>

<body >

<a href="http://www.microsoft.com">Click here to navigate to www.microsoft.com</a>

</body>

</html>
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

There is no public standard that applies to this event.

Applies To

BODY, FRAMESET, window

See Also

Introduction to Data Binding, onload, onunload
Tags What's this?: Add a tag
Community Content
 
Add Community Content
The onbeforeunload Event and the Anchor Tag      weazybird   |   Edit   |  

The onbeforeunload event is often used to warn a user that they have made changes to a web-based form if that user tries to leave the page without saving the changes. This is problematic for pages that use hyperlinks (the <a> anchor tag) to call client-side javascript, even when that code does not result in the user navigating away from the page.

For example, your page may offer a link that allows the user to manipulate content on the page through javascript and the DOM. Normally, you wouldn't want the user to be prompted because of the onbeforeunload event, but this is exactly what happens because you clicked a link and the IE browser believes you may be navigating away from the page.

You can prevent this from happening by using the onclick event of the anchor tag to prevent the onbeforeunload event from firing (set or setting a flag to allow the onbeforeunload event to continue without prompting the user.

Consider the following example where the method of setting a temporary flag is used. The flag is reset after a single use so you would need to call "NoPrompt()" from every link where onbeforeunload processing should be disabled.

<html>
<body>
<a href="javascript:alert('Hello World!');">Warning</a><br/>
<a href="javascript:alert('Hello World!');">No Warning</a>
<script>
// Allow the user to be warned by default.
var allowPrompt = true;
window.>
 
function WarnUser()
{
if(allowPrompt)
{
event.returnValue = "You have made changes. They will be lost if you continue.";
}
else
{
// Reset the flag to its default value.
allowPrompt = true;
}
}
 
function NoPrompt()
{
allowPrompt = false;
}
</script>
</body>
</html>
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker
DCSIMG