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';
};
})();
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.