Partilhar via


onbeforeunload event

Fires prior to a document being unloaded.

Syntax

HTML Attribute <element onbeforeunload = "handler(event)">
Event Property object.onbeforeunload = handler;
attachEvent Method object.attachEvent("onbeforeunload", handler)
addEventListener Method object.addEventListener("beforeunload", handler, useCapture)

 

Event information

Synchronous No
Bubbles No
Cancelable Yes

 

Event handler parameters

Standards information

There are no standards that apply here.

Remarks

When a string is assigned to the IHTMLDialog::returnValue property of window.event, a dialog box appears that gives users the option to stay on the current document 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.

onbeforeunload in Windows apps using JavaScript

In Windows apps using JavaScript, the onbeforeunload event fires before the top-level page in navigated. The IHTMLDialog::returnValue property of window.event is always ignored and onunload will fire immediately. No dialog is shown to the user and the navigation can't be cancelled. Note that, in most cases, the app should never navigate its top-level document. Windows apps using JavaScript should use oncheckpoint event to determine when they need to save state information.

General info

This event signals that the document is about to be unloaded.

To invoke this event, do one of the following:

The pEvtObj parameter is required for the following interfaces:

See also

Reference

HTMLWindowEvents::onload

HTMLWindowEvents::onunload

Conceptual

Introduction to Data Binding