gpt4 book ai didi

javascript - 启动 SweetAlert 以防止用户关闭窗口

转载 作者:数据小太阳 更新时间:2023-10-29 05:12:14 29 4
gpt4 key购买 nike

我试图在用户关闭窗口之前向他显示一条消息。我正在使用运行良好的 SweetAlert ( http://tristanedwards.me/sweetalert)。

问题在于 JavaScript/jQuery 让我知道用户何时尝试关闭窗口/选项卡,然后显示一些内容阻止他关闭页面,除非他再次单击。

<script language="JavaScript">
window.onbeforeunload = confirmExit;
function confirmExit() {
swal("Here's a message!");
return "You have attempted to leave this page. Are you sure?";
}
</script>

我试过了,但它在我的 SweetAlert 上显示了丑陋的常规消息,有什么想法吗?没有返回部分它无论如何都会关闭窗口,我试过:/

最佳答案

You can't disable the alert, nor change the style of the alert on onbeforeunload.主要原因是安全问题。

来自 MDN 文档,WindowEventHandlers.onbeforeunload ,它说,

When this event returns a non-void value, the user is prompted to confirm the page unload. In most browsers, the return value of the event is displayed in this dialog. In Firefox 4 and later the returned string is not displayed to the user. Instead, Firefox displays the string "This page is asking you to confirm that you want to leave - data you have entered may not be saved." See bug 588292.

Since 25 May 2011, the HTML5 specification states that calls to window.alert(), window.confirm(), and window.prompt() methods may be ignored during this event. See the HTML5 specification for more details.

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.

关于javascript - 启动 SweetAlert 以防止用户关闭窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29641290/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com