How to control browser confirmation dialog on leaving page?

后端 未结 2 1265
灰色年华
灰色年华 2020-12-15 09:51

I know there are a lot of questions regarding this but nothing is answering me right. I want to show a confirmation dialog when user leaves the page. If the user press Cance

相关标签:
2条回答
  • 2020-12-15 10:02

    Here's what I've done, modify to fit your needs:

    // This default onbeforeunload event
    window.onbeforeunload = function(){
        return "Do you want to leave?"
    }
    
    // A jQuery event (I think), which is triggered after "onbeforeunload"
    $(window).unload(function(){
        //I will call my method
    });
    

    Note: it's tested to work in Google Chrome, IE8 and IE10.

    0 讨论(0)
  • 2020-12-15 10:20
     window.onbeforeunload = function() {
           if (data_needs_saving()) {
               return "Do you really want to leave our brilliant application?";
           } else {
              return;
           }
        };
    
    0 讨论(0)
提交回复
热议问题