简体   繁体   English

我需要在浏览器关闭时弹出确认窗口。我正在使用onbeforeunload事件,但无法在Firefox中使用,并且还希望限制页面刷新时的弹出窗口

[英]I need a confirmation pop up on browser close.I am using onbeforeunload event but not working in Firefox also want to restrict pop up on page refresh

I need a confirmation pop up on browser close. 我需要在浏览器关闭时弹出确认信息。 I amm using onbeforeunload event but not working in Firefox also the pop up comes whenever the page refreshed which I want to restrict. 我使用onbeforeunload事件,但在Firefox中不起作用,每当刷新页面时,我都会限制弹出窗口。

window.onbeforeunload = DetectBrowserExit;

function DetectBrowserExit() {
return 'Are you sure to exit';`enter code here`
}

Not all browsers support this feature and with time browsers minimized their support (for example removed custom text support). 并非所有浏览器都支持此功能,并且随着时间的流逝,浏览器将其支持降到最低(例如,删除了自定义文本支持)。 Reason is that it is used by spam like websites that prevent you from navigating out. 原因是它被垃圾邮件之类的网站使用,从而阻止了您的浏览。

you can see full details here: 您可以在此处查看完整的详细信息:

https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload https://developer.mozilla.org/zh-CN/docs/Web/API/WindowEventHandlers/onbeforeunload

Checkout also the Browser compatibility section in that page. 还要检查该页面中的“浏览器兼容性”部分。 I would definitely suggest NOT to rely on that event and come up with alternatives that fit your website more. 我绝对建议不要依赖该事件,并提出更适合您的网站的替代方法。

specifically about firefox, see this explanation from the link I shared above: 特别是关于firefox的信息,请参阅我上面共享的链接中的以下说明:

Note also that various mobile browsers ignore the result of the event (that is, they do not ask the user for confirmation). Firefox has a hidden preference in about:config to do the same. In essence this means the user always confirms that the document may be unloaded.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM