【发布时间】:2015-12-20 22:01:40
【问题描述】:
很多程序在点击窗口退出按钮(窗口右上角的大红色 X)后会提示用户是否真的要退出。在 HTML 应用程序(HTA 文件)中,我们也可以这样做。
但是,我不知道如何取消 onbeforeunload 之后的关闭事件。我试过 event.preventDefault()、event.stopPropagation()、event.cancelBubble = true、window.event.bubbling = false,返回 false,但都不起作用。
例子:
<head>
<title>Cancel Close</title>
</head>
<script type="text/javascript">
function ask() {
var answer = confirm("Quit program?");
if (answer) {
alert("Exiting...");
} else {
//window.event.preventDefault(); //method does not exist
//window.event.stopPropagation(); //method does not exist
window.event.cancelBubble = true; //quits anyway
window.event.bubbling = false; //quits anyway
return false; //quits anyway
}
}
</script>
<body bgcolor="#ECE9D8" style="overflow:auto" onbeforeunload="ask()">
Some HTML... Click exit and cancel; the program exits anyway, but should not.
</body>
如果用户点击取消,如何取消关闭操作?
【问题讨论】:
-
使用带有边缘元的更高版本的 IE。 IE10+ 让您使用真实事件。您还需要将 beforeunload 绑定到 window,而不是 。你还需要在onbeforeunload中返回一个字符串,用户可以看到。
-
根据您的评论很难弄清楚该做什么,但最终设法做到了。一个新窗口(默认来自 IE)出现,试图确认我是否真的想退出,但同样,无论我是否取消,它都会退出。
-
这就是我害怕的,对不起。好吧,至少有了更好的 IE,您将拥有更多功能... 最后一个想法:根据您的应用程序,您可能能够序列化所有状态 onunload,然后从 onunload 事件重新启动具有所有相同设置的新应用程序。如果您想这样做,请确保
singleinstanceHTA 设置为 false。这样,应用程序仍会关闭,但它看起来好像只是在闪烁。
标签: javascript events hta