Trying to detect browser close event

后端 未结 11 729
广开言路
广开言路 2020-11-22 05:56

I have tried many methods to detect browser close event through jQuery or JavaScript. But, unfortunately, I have not been able to detect the close. The onbeforeunload<

11条回答
  •  执念已碎
    2020-11-22 06:23

    Have you tried this code?

    window.onbeforeunload = function (event) {
        var message = 'Important: Please click on \'Save\' button to leave this page.';
        if (typeof event == 'undefined') {
            event = window.event;
        }
        if (event) {
            event.returnValue = message;
        }
        return message;
    };
    
    $(function () {
        $("a").not('#lnkLogOut').click(function () {
            window.onbeforeunload = null;
        });
        $(".btn").click(function () {
            window.onbeforeunload = null;
    });
    });
    

    The second function is optional to avoid prompting while clicking on #lnkLogOut and .btn elements.

    One more thing, The custom Prompt will not work in Firefox (even in latest version also). For more details about it, please go to this thread.

提交回复
热议问题