简体   繁体   English

使用 window.confirm 而不是 onbeforeunload

[英]Using window.confirm instead of onbeforeunload

Instead of using the onbeforeunload event, is there a way I can listen for when a user tries to refresh the window or close the window, and interrupt that process with my own custom window.confirm message?除了使用onbeforeunload事件,有没有一种方法可以让我在用户尝试刷新窗口或关闭窗口时进行监听,并使用我自己的自定义window.confirm消息中断该过程? It seems that onbeforeunload custom messages are no longer supported across browsers.似乎浏览器不再支持onbeforeunload自定义消息。

true that support for onbeforeunload is fading.确实,对onbeforeunload支持正在减弱。

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

some browsers ignore the string passed in, and some ignore calls to window.alert() , window.confirm() and window.prompt() .有些浏览器会忽略传入的字符串,有些浏览器会忽略对window.alert()window.confirm()window.prompt()调用。

Many sites get around this by detecting an event that precedes window closure, such as mouseout before the user closes a tab.许多站点通过检测窗口关闭之前的事件来解决这个问题,例如用户关闭选项卡之前的mouseout

Though it's popular these days, it feels slimy, imo.虽然它现在很流行,但感觉很粘,imo。 and it can be triggered when the use is doing another action (perhaps one you want, like "bookmark this page").并且可以在用户执行其他操作时触发(可能是您想要的操作,例如“将此页面添加为书签”)。 Often I see an html5 modal used, which is less offensive than a truly blocking confirm box.我经常看到使用 html5 模式,它比真正阻止确认框的攻击性要小。 It still disrupts reading.它仍然扰乱阅读。

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

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