prevent onbeforeunload to close page in any case

醉酒当歌 提交于 2019-12-30 09:09:08

问题


I want to prevent browser to close page in any case or in other case, Prevent browser to do anything when onbeforeunload is called. Here is my code which i have tried.

 (function() {
    var proxied = window.onbeforeunload;
    window.onbeforeunload = function(e) {
        e.preventDefault();
        e.stopPropagation();
            //i want to stop everything
        console.log('stay here');
        // return 'message';
    };
})();
  • I want to perform a action before leaving the page (disconnect the chat)

回答1:


You can't outright prevent a user from leaving the page (This would lead to much abuse on spam/advertisement sites who try to get you to stay on a page), but you can show things such as a window which causes a confirm prompt to the user. Have a look at Prevent a webpage from navigating away using JavaScript which can lead you to the right direction of what you're trying to accomplish.




回答2:


There is no way to stop browser to close. The browser doesn't allow you to do that.



来源:https://stackoverflow.com/questions/30781070/prevent-onbeforeunload-to-close-page-in-any-case

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!