How can I override the OnBeforeUnload dialog and replace it with my own?

前端 未结 11 2025
一生所求
一生所求 2020-11-21 06:14

I need to warn users about unsaved changes before they leave a page (a pretty common problem).

window.onbeforeunload=handler

This works bu

相关标签:
11条回答
  • 2020-11-21 06:58

    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:

    1. When onbeforeunload is called, it will take the return value of the handler as window.event.returnValue.
    2. It will then parse the return value as a string (unless it is null).
    3. 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() {} );
      

    Edit 09/04/2018: custom messages in onbeforeunload dialogs are deprecated since chrome-51 (cf: release note)

    0 讨论(0)
  • 2020-11-21 06:58

    What about to use the specialized version of the "bind" command "one". Once the event handler executes the first time, it’s automatically removed as an event handler.

    $(window).one("beforeunload", BeforeUnload);
    
    0 讨论(0)
  • 2020-11-21 07:02

    I faced the same problem, I was ok to get its own dialog box with my message, but the problem I faced was : 1) It was giving message on all navigations I want it only for close click. 2) with my own confirmation message if user selects cancel it still shows the browser's default dialog box.

    Following is the solutions code I found, which I wrote on my Master page.

    function closeMe(evt) {
        if (typeof evt == 'undefined') {
            evt = window.event; }
        if (evt && evt.clientX >= (window.event.screenX - 150) &&
            evt.clientY >= -150 && evt.clientY <= 0) {
            return "Do you want to log out of your current session?";
        }
    }
    window.onbeforeunload = closeMe;
    
    0 讨论(0)
  • 2020-11-21 07:03

    While there isn't anything you can do about the box in some circumstances, you can intercept someone clicking on a link. For me, this was worth the effort for most scenarios and as a fallback, I've left the unload event.

    I've used Boxy instead of the standard jQuery Dialog, it is available here: http://onehackoranother.com/projects/jquery/boxy/

    $(':input').change(function() {
        if(!is_dirty){
            // When the user changes a field on this page, set our is_dirty flag.
            is_dirty = true;
        }
    });
    
    $('a').mousedown(function(e) {
        if(is_dirty) {
            // if the user navigates away from this page via an anchor link, 
            //    popup a new boxy confirmation.
            answer = Boxy.confirm("You have made some changes which you might want to save.");
        }
    });
    
    window.onbeforeunload = function() {
    if((is_dirty)&&(!answer)){
                // call this if the box wasn't shown.
        return 'You have made some changes which you might want to save.';
        }
    };
    

    You could attach to another event, and filter more on what kind of anchor was clicked, but this works for me and what I want to do and serves as an example for others to use or improve. Thought I would share this for those wanting this solution.

    I have cut out code, so this may not work as is.

    0 讨论(0)
  • 2020-11-21 07:04

    This can't be done in chrome now to avoid spamming, refer to javascript onbeforeunload not showing custom message for more details.

    0 讨论(0)
  • 2020-11-21 07:06

    What worked for me, using jQuery and tested in IE8, Chrome and Firefox, is:

    $(window).bind("beforeunload",function(event) {
        if(hasChanged) return "You have unsaved changes";
    });
    

    It is important not to return anything if no prompt is required as there are differences between IE and other browser behaviours here.

    0 讨论(0)
提交回复
热议问题