16

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
2
  • How did you do you call to this alert?
    – Sertage
    Aug 2, 2016 at 17:32
  • 1
    I'm not calling it. Chrome is by default throwing this error. It comes when I'm opening the form, otherwise everything is going great.
    – Abhishek
    Aug 6, 2016 at 7:29

6 Answers 6

16

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;
3
  • 4
    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
    – Abhishek
    Dec 22, 2016 at 7:00
  • 1
    Can't you override that function?
    – Khom Nazid
    Sep 26, 2018 at 0:13
  • 1
    window.onbeforeunload = yourCustomFunction; should work Nov 6, 2018 at 3:42
2

You can hook in any other function inside the beforeunload handler:

window.addEventListener("beforeunload", function(e){
    yourCustomFunction();
});
2

Set it to an empty function:

window.onbeforeunload = () => {}

0

Enclose your form tag inside div like this:

<div class="col-xs-12 no-padding"><form role="form" class="form-horizontal pad10-top" id="" onSubmit="return false;"> ...</form></div>

0

You can try this as well.

$(window).off('beforeunload');

0

adding this line got me through

window.addEventListener("beforeunload", function (e) { return true; });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.