单击窗口退出按钮(窗口右上角的大红色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>


如果用户单击“取消”,如何取消关闭操作?

最佳答案

我认为这在.hta应用程序中是不可能的。您可以尝试其他解决方案来隐藏关闭按钮并在页面中创建关闭按钮。这是您修改的示例代码:

<head>
<title>Cancel Close</title>
<hta:application sysmenu="no"/>
</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>


您可以检查应用程序对象引用
https://msdn.microsoft.com/en-us/library/ms536495%28v=vs.85%29.aspx
这些.htа应用程序有非常具体的内容。

关于javascript - 用户单击关闭按钮后取消HTA关闭,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34386368/

10-11 23:03
查看更多