JavaScript before leaving the page

前端 未结 10 1755
谎友^
谎友^ 2020-11-22 08:01

I want to make a confirmation before user leaving the page. If he says ok then it would redirect to new page or cancel to leave. I tried to make it with onunload

<         


        
相关标签:
10条回答
  • 2020-11-22 08:22

    onunload (or onbeforeunload) cannot redirect the user to another page. This is for security reasons.

    If you want to show a prompt before the user leaves the page, use onbeforeunload:

    window.onbeforeunload = function(){
      return 'Are you sure you want to leave?';
    };
    

    Or with jQuery:

    $(window).bind('beforeunload', function(){
      return 'Are you sure you want to leave?';
    });
    

    This will just ask the user if they want to leave the page or not, you cannot redirect them if they select to stay on the page. If they select to leave, the browser will go where they told it to go.

    You can use onunload to do stuff before the page is unloaded, but you cannot redirect from there (Chrome 14+ blocks alerts inside onunload):

    window.onunload = function() {
        alert('Bye.');
    }
    

    Or with jQuery:

    $(window).unload(function(){
      alert('Bye.');
    });
    
    0 讨论(0)
  • 2020-11-22 08:23

    Normally you want to show this message, when the user has made changes in a form, but they are not saved.

    Take this approach to show a message, only when the user has changed something

    var form = $('#your-form'),
      original = form.serialize()
    
    form.submit(function(){
      window.onbeforeunload = null
    })
    
    window.onbeforeunload = function(){
      if (form.serialize() != original)
        return 'Are you sure you want to leave?'
    }
    
    0 讨论(0)
  • 2020-11-22 08:32

    This will alert on leaving current page

    <script type='text/javascript'>
    function goodbye(e) {
        if(!e) e = window.event;
        //e.cancelBubble is supported by IE - this will kill the bubbling process.
        e.cancelBubble = true;
        e.returnValue = 'You sure you want to leave?'; //This is displayed on the dialog
    
        //e.stopPropagation works in Firefox.
        if (e.stopPropagation) {
            e.stopPropagation();
            e.preventDefault();
        }
    }
    window.onbeforeunload=goodbye; 
    
    </script>
    
    0 讨论(0)
  • 2020-11-22 08:33

    This code when you also detect form state changed or not.

    $('#form').data('serialize',$('#form').serialize()); // On load save form current state
    
    $(window).bind('beforeunload', function(e){
        if($('#form').serialize()!=$('#form').data('serialize'))return true;
        else e=null; // i.e; if form state change show warning box, else don't show it.
    });
    

    You can Google JQuery Form Serialize function, this will collect all form inputs and save it in array. I guess this explain is enough :)

    0 讨论(0)
  • 2020-11-22 08:34

    This what I did to show the confirmation message just when I have unsaved data

    window.onbeforeunload = function () {
                if (isDirty) {
                    return "There are unsaved data.";
                }
                return undefined;
            }
    

    returning "undefined" will disable the confirmation

    Note: returning "null" will not work with IE

    Also you can use "undefined" to disable the confirmation

    window.onbeforeunload = undefined;
    
    0 讨论(0)
  • 2020-11-22 08:37

    In order to have a popop with Chrome 14+, you need to do the following :

    jQuery(window).bind('beforeunload', function(){
        return 'my text';
    });
    

    The user will be asked if he want to stay or leave.

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