javascript检测是否在hta中运行

javascript检测是否在hta中运行,javascript,web-applications,hta,Javascript,Web Applications,Hta,我们希望允许我们的用户下载hta文件并在其中运行我们的web应用程序,让某些页面检测到他们正在hta文件中运行,并提供web应用程序通常没有权限执行的其他功能 如何简单地检测页面是否正在从hta文件容器中浏览?我没有测试,但不会只查看window.location工作?window.location.protocol=='file:'将指示本地页面,但可能是本地页面 html页面或本地hta 我在想窗口。外部的在不同的上下文中可能是不同的。 因此,制作和打开a.htm和a.hta包含: <

我们希望允许我们的用户下载hta文件并在其中运行我们的web应用程序,让某些页面检测到他们正在hta文件中运行,并提供web应用程序通常没有权限执行的其他功能


如何简单地检测页面是否正在从hta文件容器中浏览?

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

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

我在想
窗口。外部的
在不同的上下文中可能是不同的。
因此,制作和打开
a.htm
a.hta
包含:

<script>document.write(window.external)</script>
document.write(窗口外部)
我们得到:

  • IE:
    [对象]
  • FireFox:
    [xpconnect-wrapped(nsISupports、nsISidebar、nsISidebarExternal、nsIClassInfo)]
  • Chrome:
    [对象]
  • HTA:
    null
因此,
isHTA=(window.external==null)
将指示HTA上下文

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


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

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

<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>

警报(“isHTA=”+isHTA(“myHTA”);
功能isHTA(htaId){
var-retval=false;
var hta=窗口[htaId];
如果(!hta){
//未定义hta
}else if(hta.scopeName!=“hta”){
//hta:应用程序
}else if(hta.nodeName!=“应用程序”){
//hta:应用程序
}else if(hta.tagName!=“应用程序”){
//hta:应用程序
}否则{
retval=true;
//只有真正的hta才会具有的属性
var attribKeys=[
“应用程序名称”,
“边界”,
“边界样式”,
“标题”,
“命令行”,
“上下文菜单”,
“图标”,
“内边界”,
“最大化按钮”,
“最小化按钮”,
“卷轴”,
“滚平”,
“选择”,
“showInTaskBar”,
“单实例”,
“系统菜单”,
“版本”,
“窗口状态”
];
对于(var i=0;i那么仅仅:-

 var isHTA = (document.all && top.document && (top.document.getElementsByTagName('application')[0]));

HTA使用标记填充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);

检查HTA应用程序对象的
commandLine
属性是查看您是否作为真正的HTML应用程序运行的最佳方法,因为此属性仅在mshta.exe中可用

需要获取HTM应用程序对象才能检查此属性。如果不知道对象的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";
}

我想现在仍然使用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>

var VBScriptVersion=“”;
函数getVBScriptVersion(){
var firstScriptBlock=document.getElementsByTagName('script')[0];
var tmpScript=document.createElement('script');
setAttribute(“语言”、“VBScript”);
tmpScript.text='VBScriptVersion=scriptenginemajorvision&'&ScriptEngineMinorVersion';
tmpScript.async=false;
tmpScript.onload=函数(){
this.parentNode.removeChild(this);
}
firstScriptBlock.parentNode.insertBefore(tmpScript,firstScriptBlock);
返回VBScriptVersion;
}
var isHTA=(getVBScriptVersion()!=“”&&window.external==null);

否。尽管应用程序是通过hta文件(包含登录屏幕)打开的,一旦登录,用户将浏览web应用程序的普通html文件。Window.location仅提供用户当前位置。这仅在您知道正在使用的hta的id时起作用。其他浏览器支持运行hta文件?rjmunro:我不知道除了支持hta的mshta.exe之外还有什么其他内容。但是,该标记在DOM。当从HTTP服务器提供HTA时,我发现这很有用-我可以将它们作为HTML文档,在其他浏览器中显示如何将其作为HTA运行的说明。这非常有效,但遗憾的是在windows上的safari
窗口中。external
也为null!:(此返回为false(尽管HTA可用)当在从HTA应用程序打开的弹出窗口中运行时,您还必须检查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>