启动 SweetAlert 以防止用户关闭 window
Launching SweetAlert to prevent user from closing window
我试图在用户关闭 window 之前向他显示一条消息。我正在使用 SweetAlert (http://tristanedwards.me/sweetalert),运行 很好。
问题在于 JavaScript/jQuery 让我知道用户何时试图关闭 window/tab 然后显示阻止他关闭页面的内容,除非他再次单击。
<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 上显示了丑陋的常规消息,有什么想法吗?
没有 return 部分它会关闭 window 无论如何,我试过:/
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.
我试图在用户关闭 window 之前向他显示一条消息。我正在使用 SweetAlert (http://tristanedwards.me/sweetalert),运行 很好。
问题在于 JavaScript/jQuery 让我知道用户何时试图关闭 window/tab 然后显示阻止他关闭页面的内容,除非他再次单击。
<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 上显示了丑陋的常规消息,有什么想法吗? 没有 return 部分它会关闭 window 无论如何,我试过:/
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.