【问题标题】:cancel HTA close after user clicks close button用户单击关闭按钮后取消 HTA 关闭
【发布时间】:2015-12-20 22:01:40
【问题描述】:

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

但是,我不知道如何取消 onbeforeunload 之后的关闭事件。我试过 event.preventDefault()event.stopPropagation()event.cancelBubble = truewindow.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 事件重新启动具有所有相同设置的新应用程序。如果您想这样做,请确保 singleinstance HTA 设置为 false。这样,应用程序仍会关闭,但它看起来好像只是在闪烁。

标签: javascript events hta


【解决方案1】:

我认为这在 .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а 应用程序有一些非常具体的内容。

【讨论】:

  • 是的,我知道 htа:application 标签,创建自定义按钮是个好主意。但是我会失去最大化和最小化按钮,我不知道要重新创建它们的功能。有什么线索吗?
  • 我找到了一种使用内部按钮和 javascript 最大化和最小化的方法。所以,这个答案就是我要实现的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-15
  • 1970-01-01
  • 1970-01-01
  • 2019-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多