Prevent onbeforeunload to close the page anyway

I want the browser to close the page anyway or otherwise. Prevent the browser from doing anything when called onbeforeunload

. Here is my code that 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 an action before leaving the page (disable chat)
+3


source to share


2 answers


You cannot completely prevent a user from leaving the page (this will lead to a lot of abuse by spam / ad sites that try to force you to stay on the page), but you can show things like a window that prompts the user to confirm. Check out this post which might lead you in the right direction for what you are trying to accomplish.



+6


source


Unable to stop browser. The browser won't let you do this.



+3


source







All Articles