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

前端 未结 11 2032
一生所求
一生所求 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 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;
    

提交回复
热议问题