Javascript 我怎样才能摆脱“你确定要离开这个页面”的消息吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13973136/
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
How can I get rid of the "Are you sure you want to leave this page" message?
提问by ?ngelo Rigo
How can I get rid of the "Are you sure you want to leave this page" message?
我怎样才能摆脱“你确定要离开这个页面”的消息吗?
I try to use window.onBeforeunload=nulland it works for Chrome, but it did not work for Firefox, Internet Explorer and Opera.
我尝试使用window.onBeforeunload=null它,它适用于 Chrome,但它不适用于 Firefox、Internet Explorer 和 Opera。
Thanks in advance.
提前致谢。
回答by Teemu
I'm not sure why your script is working in Chrome, all browsers should behave in the same way with this. Is it possible, that the code block where you remove the event listener, is for some reason executed only in Chrome?
我不确定为什么您的脚本在 Chrome 中工作,所有浏览器的行为方式都应该与此相同。您删除事件侦听器的代码块是否可能出于某种原因仅在 Chrome 中执行?
Anyway, if you set window.onbeforeunload = someFunction;you can nullify it with window.onbeforeunload = null. However, if you set window.addEventListener('beforeunload', someFunction);, this event listener can't be removed with window.onbeforeunload = null. It can be removed only with removeEventListener('beforeunload', someFunction);.
无论如何,如果你设置了window.onbeforeunload = someFunction;你可以用window.onbeforeunload = null. 但是,如果您设置了window.addEventListener('beforeunload', someFunction);,则无法使用 删除此事件侦听器window.onbeforeunload = null。它只能用 删除removeEventListener('beforeunload', someFunction);。
If this answer doesn't help, please post all relevant code, like the snippet when assigning event listener, and also when trying to remove it.
如果此答案没有帮助,请发布所有相关代码,例如分配事件侦听器时以及尝试删除它时的代码段。
回答by wickdninja
Using jQuery
使用 jQuery
$(window).off('beforeunload'); // tested in IE 11 and Chrome 62
回答by user57510
Clear my browsing history, seemed to help.
清除我的浏览历史记录,似乎有帮助。

