Javascript 如何禁用/覆盖“你想离开这个网站吗?” 警报?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38723567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:46:23  来源:igfitidea点击:

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

javascriptjqueryruby-on-railsajax

提问by Abhishek

I've a form in a pop-up, which is loaded by AJAX call. It is built using form_fortag 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.

我在弹出窗口中有一个表单,它由 AJAX 调用加载。它是使用form_forRoR 标签构建的。即使我没有修改其中的任何字段并尝试导航到另一个页面,chrome 也会显示以下警报。

enter image description here

在此处输入图片说明

I want to disablethis 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

回答by Federico Saenz

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;

回答by Marian Zburlea

Set it to an empty function:

将其设置为空函数:

window.onbeforeunload = () => {}

window.onbeforeunload = () => {}

回答by Bowen Li

You can hook in any other function inside the beforeunloadhandler:

您可以在beforeunload处理程序中挂钩任何其他函数:

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

回答by Katta Nagarjuna

Enclose your formtag inside divlike this:

像这样将您的form标签括在里面div

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

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