Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Questo articolo richiede una revisione tecnica. Scopri come puoi essere d’aiuto.

I volontari di MDN non hanno ancora tradotto questo articolo in Italiano. Registrati per tradurlo tu.

 

The WindowEventHandlers.onbeforeunload event handler property contains the code executed when the beforeunload is sent. This event fires when a window is about to unload its resources. The document is still visible and the event is still cancelable.

Note: To combat unwanted pop-ups, browsers may not display prompts created in beforeunload event handlers unless the page has been interacted with. For a list of specific browsers, see the Browser compatibility section.

Syntax

window.onbeforeunload = funcRef
  • funcRef is a reference to a function or a function expression.
  • The function should assign a string value to the returnValue property of the Event object and return the same string.

Example

window.onbeforeunload = function(e) {
  var dialogText = 'Dialog text here';
  e.returnValue = dialogText;
  return dialogText;
};

Notes

When this event returns an undefined value, the user is prompted to confirm the page unload. In most browsers, the return value of the event is displayed in this dialog. In Firefox 4 and later the returned string is not displayed to the user. Instead, Firefox displays the string "This page is asking you to confirm that you want to leave - data you have entered may not be saved." See bug 588292.

Since 25 May 2011, the HTML5 specification states that calls to window.alert(), window.confirm(), and window.prompt() methods may be ignored during this event. See the HTML5 specification for more details.

Note also that various mobile browsers ignore the result of the event (that is, they do not ask the user for confirmation). Firefox has a hidden preference in about:config to do the same. In essence this means the user always confirms that the document may be unloaded.

You can and should handle this event through window.addEventListener() and the beforeunload event. More documentation is available there.

Specifications

The event was originally introduced by Microsoft in Internet Explorer 4 and standardized in the HTML5 specification.

Specification Status Comment
WHATWG HTML Living Standard
The definition of 'GlobalEventHandlers' in that specification.
Living Standard  
HTML5.1
The definition of 'GlobalEventHandlers' in that specification.
Working Draft  
HTML5
The definition of 'GlobalEventHandlers' in that specification.
Recommendation  

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 1.0 1 4 12 3
Custom text support removed 51.0 44.0 (44.0)   38 9.1
Feature Android Android Webview Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile Chrome for Android
Basic support ? (Yes) ? ? ? (no) defect (Yes)
Custom text support removed ? 51.0 44.0 (44.0)       51.0

See also

Tag del documento e collaboratori

 Ultima modifica di: SRachamim,