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
Community
  • 1
  • 1
Abhishek
  • 6,912
  • 14
  • 59
  • 85
  • How did you do you call to this alert? – Sertage Aug 02 '16 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 06 '16 at 07:29
  • This isn't Chrome, it's something else (that is telling Chrome to do this), possibly rails-ujs, though I don't remember if that existed in Rails 3 (which is really old at this point) or whether this would have been default configuration. Basically, there should be some gem or something that is doing this, and you can probably tell it not to, but without more information I'm not able to guess what's doing it, and you probably do not just want to do something like blanket intercept all beforeunload and cancel them because that could affect other things. – cesoid May 26 '23 at 14:52

6 Answers6

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;
Federico Saenz
  • 512
  • 1
  • 6
  • 21
2

Set it to an empty function:

window.onbeforeunload = () => {}

Kos
  • 4,890
  • 9
  • 38
  • 42
Marian Zburlea
  • 9,177
  • 4
  • 31
  • 37
2

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

window.addEventListener("beforeunload", function(e){
    yourCustomFunction();
});
Kos
  • 4,890
  • 9
  • 38
  • 42
Bowen Li
  • 387
  • 1
  • 8
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>

Kos
  • 4,890
  • 9
  • 38
  • 42
Katta Nagarjuna
  • 1,539
  • 2
  • 12
  • 12
0

You can try this as well.

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

TauFeeQ
  • 153
  • 1
  • 11
0

adding this line got me through

window.addEventListener("beforeunload", function (e) { return true; });
dersvenhesse
  • 6,276
  • 2
  • 32
  • 53
Naddy
  • 1