启动SweetAlert以防止用户关闭窗口 [英] Launching SweetAlert to prevent user from closing window

查看:671
本文介绍了启动SweetAlert以防止用户关闭窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to display a message to the user before he closes the window. I am using SweetAlert (http://tristanedwards.me/sweetalert) which is running fine.

JavaScript/jQuery的问题在于,当用户试图关闭窗口/选项卡时,我会通知我,然后显示一些阻止他关闭页面的内容,除非他再次单击.

The problem is with the JavaScript/jQuery to let me know when the user is trying to close the window/tab and then to display something preventing him from closing the page unless he clicks again.

<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顶部显示了通常的丑陋消息,有什么想法吗? 没有返回部分,它无论如何都会关闭窗口,我尝试过:/

I have tried this, but it displays the ugly usual message on top of my SweetAlert, any ideas? Without the return part it closes the window anyway, I tried :/

推荐答案

You can't disable the alert, nor change the style of the alert on onbeforeunload. The main reason is for security problems.

从MDN文档开始, WindowEventHandlers.onbeforeunload ,它说,

From MDN document, WindowEventHandlers.onbeforeunload, it says,

当此事件返回非空值时,将提示用户确认页面卸载.在大多数浏览器中,事件的返回值显示在此对话框中.在Firefox 4及更高版本中,返回的字符串不会显示给用户.而是,Firefox显示字符串此页面要求您确认要离开-输入的数据可能不会保存."看到错误588292.

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.

自2011年5月25日以来,HTML5规范规定在此事件期间可能会忽略对window.alert(),window.confirm()和window.prompt()方法的调用.有关更多详细信息,请参见HTML5规范.

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.

还请注意,各种移动浏览器都忽略事件的结果(即,它们不要求用户进行确认). Firefox在about:config中具有隐藏的首选项以执行相同的操作.从本质上讲,这意味着用户始终会确认文档可能已被卸载.

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.

这篇关于启动SweetAlert以防止用户关闭窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆