How to control browser confirmation dialog on leaving page?

后端 未结 2 1272
灰色年华
灰色年华 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.

提交回复
热议问题