HBCMS更多的中文手册参考:

onbeforeunload Event

DHTML文档教程

Fires prior to a page being unloaded.

dhtml语法

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 a that refers the browser to another web page.
  • invoke the a?a href="../methods/click.html">click method.
  • invoke the document?a href="../methods/write.html">write method.
  • invoke the document?a href="../methods/open_1.html">open method.
  • invoke the document?a href="../methods/close_1.html">close method.
  • invoke the window?a href="../methods/close_0.html">close method.
  • invoke the window?a href="../methods/open_0.html">open method, providing the possible value _self for the window name.
  • invoke the window?a href="../methods/navigate.html">navigate or navigateandfind method.
  • invoke the location?a href="../methods/replace.html">replace method.
  • invoke the location?a href="../methods/reload.html">reload method.
  • specify a new value for the location?a href="../properties/href_3.html">href property.
  • submit a form to the address specified in the action attribute via the input type=submit control, or invoke the form?a href="../methods/submit.html">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 an event object for data.

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.

DHTML代码范例

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>
function closeIt()
{
event.returnValue = "Any string value here forces a dialog box to \
appear before closing the window.";
}
</SCRIPT>
</HEAD>
<BODY onbeforeunload="closeIt()">
<a href="/hbcms/php/r.php?url=http%3a%2f%2fwww.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.

是否符合公共标准

There is no public standard that applies to this event.

Applies To

BODY, FRAMESET, window

更多语法参考

introduction to data binding, onload, onunload