How to disable/override "Do you want to leave this site?" alert?
Solution 1
Alert is displayed because somewhere on your code, you are overriding the window before unload event, and when you try to close the window, the event fires. Try disallow this event putting up this on your code:
window.onbeforeunload = null;
Solution 2
You can hook in any other function inside the beforeunload
handler:
window.addEventListener("beforeunload", function(e){
yourCustomFunction();
});
Solution 3
Set it to an empty function:
window.onbeforeunload = () => {}
Abhishek
I'm a polygot software developer trying to learn , learn & implement! :)
Updated on July 24, 2022Comments
-
Abhishek almost 2 years
I've a form in a pop-up, which is loaded by AJAX call. It is built using
form_for
tag of RoR. Even if I don't modify any field in it and try to navigate to another page, following alert is shown by chrome.I want to disable this alert box. Is it possible? If yes, how?
I've already tried this, but it is not valid anymore.
Following are the environment settings,
Ruby version = 1.9.3 Rails version = 3.1.4 Chrome version = 52 jQuery version = 1.10.2
-
Abhishek over 7 yearsbut this will remove all the
onbeforeunload
events. I want to run code which I've specified inonbeforeunload
event, but not chrome's version -
Khom Nazid over 5 yearsCan't you override that function?
-
éclairevoyant over 5 years
window.onbeforeunload = yourCustomFunction;
should work