用户单击关闭按钮后取消 HTA 关闭

cancel HTA close after user clicks close button

很多程序在点击window退出按钮(window右上角的红色大X)后,会提示用户是否真的要退出。在 HTML 应用程序(HTA 文件)中,我们也可以做到。

但是,我不知道如何取消onbeforeunload之后的close事件。我试过 event.preventDefault(), event.stopPropagation(), event.cancelBubble = true window.event.bubbling = falsereturn 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а 应用程序有非常具体的内容。