Results 1 to 2 of 2

Thread: onbeforeunload event parameter and empty return value

  1. #1
    Join Date
    Nov 2009
    Location
    Isfahan, Iran
    Posts
    229
    Thanks
    46
    Thanked 1 Time in 1 Post

    Default onbeforeunload event parameter and empty return value

    Q1: I have seen the following code in most sources:

    Code:
    window.onbeforeunload = function () {
        return 'You might lose your data!';
    };
    For example:

    1. beforeunload | onbeforeunload event (Internet Explorer)
    2. onbeforeunload event | beforeunload event JavaScript
    3. javascript - How can I override the OnBeforeUnload dialog and replace it with my own? - Stack Overflow


    But on MDN the function has an extra parameter: (e). Why is that, what is the difference, and which is right?


    Q2: It seems that any browser has a default confirmation message that you cannot customize. Chrome, for example, displays:

    Are you sure you want to leave this page?
    Can I content myself to this message and leave the return value empty:

    Code:
    return '';

  2. #2
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    512
    Thanks
    9
    Thanked 61 Times in 59 Posts

    Default

    You can't modify the default dialogue for onbeforeunload, so your best bet may be to work with it.

    window.onbeforeunload = function() {
    return 'You have unsaved changes!';
    }
    Here's a reference to this from Microsoft:

    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.

    The problem seems to be:

    When onbeforeunload is called, it will take the return value of the handler as window.event.returnValue.
    It will then parse the return value as a string (unless it is null).
    Since false is parsed as a string, the dialogue box will fire, which will then pass an appropriate true/false.
    The result is, there doesn't seem to be a way of assigning false to onbeforeunload to prevent it from the default dialogue.

    Additional notes on jQuery:

    Setting the event in jQuery may be problematic, as that allows other onbeforeunload events to occur as well. If you wish only for your unload event to occur I'd stick to plain ol' JavaScript for it.
    jQuery doesn't have a shortcut for onbeforeunload so you'd have to use the generic bind syntax.

    $(window).bind('beforeunload', function() {} );
    -DW [Deadweight]
    Resolving your thread: First Post: => EDIT => Lower right: => GO ADVANCED => Top Advance Editor drop down: => PREFIX:Resolved

Similar Threads

  1. Replies: 8
    Last Post: 08-27-2009, 05:21 AM
  2. Window onbeforeunload
    By icesolid in forum JavaScript
    Replies: 0
    Last Post: 05-13-2008, 07:51 PM
  3. Swiss Army slideshow: onclick event and specs parameter conflict
    By richy in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 10-17-2007, 12:57 PM
  4. Replies: 5
    Last Post: 08-09-2007, 11:11 AM
  5. onbeforeunload
    By mathurneni in forum JavaScript
    Replies: 2
    Last Post: 07-04-2005, 08:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •