2

We'd like to allow our users to download an hta file and run our web app inside it, and have certain pages detect that they are running in an hta file and offer additional features that a web app wouldn't normally have permission to do.

How can I simply detect if the page is being browsed from an hta file container?

4

7 回答 7

4

window.location.protocol=='file:'将指示本地页面,但可能是本地 html 页面或本地 hta。

我想window.external在每种情况下可能会有所不同。所以制作、打开a.htma.hta包含:

<script>document.write(window.external)</script>

我们得到:

  • IE:[object]
  • 火狐:[xpconnect wrapped (nsISupports, nsISidebar, nsISidebarExternal, nsIClassInfo)]
  • 铬合金:[object Object]
  • HTA:null

因此,isHTA=(window.external==null)将指示 HTA 上下文。

或者,isHTA=false;try{isHTA=(window.external==null)}catch(e){}

为了安全起见,因为我只测试了当前版本的 IE、FF 和 Chrome,谁知道其他浏览器会做什么。

于 2011-05-05T02:47:47.533 回答
2

只是:-

 var isHTA = (document.all && top.document && (top.document.getElementsByTagName('application')[0]));
于 2011-08-09T07:59:58.377 回答
1

HTA 在如何使用 <HTA:APPLICATION> 标记填充 DOM 方面是独一无二的。我使用以下内容来获取 HTA 对象:

var hta;
var elements = document.getElementsByTagName("APPLICATION");
for(var i=0; i<elements.length; i+=1) {
    if ("hta" === elements[i].scopeName.toString().toLowerCase()) {
        hta = elements[i];
        break;
    }
}

// To test if the page is an HTA:
var isHta = (undefined !== hta);

在其他浏览器中,您将不得不使用完整的标签名称来访问同一个对象:

// For Firefox/Chrome/IE
var elements = document.getElementsByTagName("HTA:APPLICATION");
于 2013-01-21T21:34:57.383 回答
0

检查commandLineHTA-Application 对象的属性是查看您是否作为真正的 HTML-Application 运行的最佳方法,因为此属性仅在 mshta.exe 中可用。

您需要获取 HTM-Application 对象来检查此属性。如果您不知道对象的 ID,可以使用以下代码:

// Check if running in a HTML-Application
var isHTA = false;
var htaApp = document.getElementsByTagName("HTA:APPLICATION")
if (!htaApp.length) {
    htaApp = document.getElementsByTagName("APPLICATION");
}
if (htaApp.length == 1 && htaApp[0]) {
    isHTA = typeof htaApp[0].commandLine !== "undefined";
}
于 2013-03-12T10:00:07.657 回答
0

我没有测试过,但不会只看 window.location 工作吗?

于 2010-04-29T11:52:18.213 回答
0

这可能符合要求。可以删除验证属性。

<hta:application id="myHTA"/>
<script>
alert("isHTA = " + isHTA("myHTA"));

function isHTA(htaId) {
  var retval = false;
  var hta = window[htaId];
  if (!hta) {
    // hta wasn't defined
  } else if (hta.scopeName != "hta") {
    // hta:application
  } else if (hta.nodeName != "application") {
    // hta:application
  } else if (hta.tagName != "application") {
    // hta:application
  } else {
    retval = true;
    // attributes only a real hta would have
    var attribKeys = [
      "applicationName",
      "border",
      "borderStyle",
      "caption",
      "commandLine",
      "contextMenu",
      "icon",
      "innerBorder",
      "maximizeButton",
      "minimizeButton",
      "scroll",
      "scrollFlat",
      "selection",
      "showInTaskBar",
      "singleInstance",
      "sysMenu",
      "version",
      "windowState"
    ];
    for (var i=0;i<attribKeys.length;i++) {
      var attribKey = attribKeys[i];
      if (!hta.attribKey === undefined) {
        retval = false;
        break;
      }
    }
  }
  return retval;
}

</script>
于 2011-08-06T15:54:57.177 回答
0

估计现在还没有多少人还在使用 HTA,无论如何,我认为下面应该涵盖所有场景:

<script language=javascript>
  var VBScriptVersion = "";
  function getVBScriptVersion() {
    var firstScriptBlock = document.getElementsByTagName('script')[0];
    var tmpScript = document.createElement('script');
      tmpScript.setAttribute("language", "VBScript");
      tmpScript.text = 'VBScriptVersion = ScriptEngineMajorVersion & "." & ScriptEngineMinorVersion';
      tmpScript.async = false;
      tmpScript.onload = function() {
        this.parentNode.removeChild(this);
      }
    firstScriptBlock.parentNode.insertBefore(tmpScript, firstScriptBlock);
    return VBScriptVersion;
  }

  var isHTA = (getVBScriptVersion()!="" && window.external==null);
</script>
于 2019-05-03T05:54:46.223 回答