Your Search Results

    window.onbeforeunload

    Summary

    An event that fires when a window is about to unload its resources. The document is still visible and the event is still cancelable.

    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.
    • Note that 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.

    Example

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

    Notes

    When this event returns a non-void value, the user is prompted to confirm the page unload. In most browsers, the return value of the event is displayed in this dialog.

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

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

    Browser compatibility

    Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
    Basic support 1 1 4 12 3
    Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
    Basic support ? ? ? ? (no) defect

    Specification

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

    See also

    Document Tags and Contributors

    Last updated by: tgs,
    Hide Sidebar