How to disable/override "Do you want to leave this site?" alert?

31,981

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 = () => {}

Share:
31,981
Abhishek
Author by

Abhishek

I'm a polygot software developer trying to learn , learn & implement! :)

Updated on July 24, 2022

Comments

  • Abhishek
    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.

    enter image description here

    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
    Abhishek over 7 years
    but this will remove all the onbeforeunload events. I want to run code which I've specified in onbeforeunload event, but not chrome's version
  • Khom Nazid
    Khom Nazid over 5 years
    Can't you override that function?
  • éclairevoyant
    éclairevoyant over 5 years
    window.onbeforeunload = yourCustomFunction; should work