Skip to content
Advertisement

Jump to anchor in HTA file by command line specified parameter

Have cmd script help file in HTA format, it open every time user press key ‘h/H’ through script. Want to HTA jump to its specific part by context from where help is called (as usual in windows help files), but not sure how to do it. Thinking to pass anchor name as a parameter of mshta.exe, and than process in the beginning of HTA file…

Standard HTML defined anchors do not work in my case (maybe this does not work in HTA at all ??), but found working javascript way to do it.

<head>
<script type="text/javascript">
    function spoil(id){
        if (document.getElementById) {
            var divid = document.getElementById(id);
            divid.style.display = (divid.style.display = 'block');
            window.location = '#' + id;
        }
    }
</script>
</head>
<body onload="spoil('anchor_name')";>

Also found a way how to read passed parameters of mshta.exe, but it is in VBS and I have no idea how to transfer variable from VBS to javascript.

Anyone help me to make different language scripts to collaborate or propose other way to achive %subj%

Advertisement

Answer

About jumping to anchor:

For me, the regular way works just fine. I tried window.location = '#stuff', window.location.href = '#stuff' and window.location.hash = '#stuff', all of which had the desired effect.


About reading command line:

You should be able to use the same mechanism described in the linked tutorial – give the <hta:application> tag an id and then refer to it in JavaScript as and read its commandLine property:

<html>
  <head>
    <title>Test</title>
    <hta:application id="htaApp" applicationname="Test">
    
    <script language="JScript">
      window.onload = function () {
        var htaApp = document.getElementById('htaApp')
        document.getElementById('cmdLine').innerText = htaApp.commandLine
      }
    </script>
  </head>
  
  <body>
    <p>Command line: <span id="cmdLine"></span></p>
  </body>
</html>

In the command line, you get the whole thing as string, which can be problematic if you don’t know where the “argument 0” (the file path) ends and where the “actual” arguments start, since there can be spaces in the path.

A heuristic method that should work in all cases you encounter in the wild would be looking for .hta followed by an optional doublequote and then either end of string or some whitespace, and then only looking at the part that follows that.

var actualArgumentsPart = htaApp.commandLine.replace(/^.*?.hta"?(s+|$)/, '')

If you wanted, you could then also split that part into individual arguments on whitespace (which would ignore quotes in arguments though – if quotes need to be respected, you’d need a function like this instead):

var individualArguments = actualArgumentsPart.split(/s+/)

I think this isn’t even necessary in your case though, so you can simply do this:

<html>
  <head>
    <title>Test</title>
    <hta:application id="htaApp" applicationname="Test">
    
    <script language="JScript">
      window.onload = function () {
        var htaApp = document.getElementById('htaApp')
        var htaArguments = htaApp.commandLine.replace(/^.*?.hta"?(s+|$)/, '')
        window.location.hash = '#' + htaArguments
      }
    </script>
  </head>
  <body>
    This is an example!    

    <h1 id="first">First Header</h1>
    <script language="JScript">for (var i = 0; i < 1000; i++) document.write('Lorem Ipsum ');</script>
    
    <h1 id="second">Second Header</h1>
    <script language="JScript">for (var i = 0; i < 1000; i++) document.write('Lorem Ipsum ');</script>
    
    <h1 id="third">Third Header</h1>
    <script language="JScript">for (var i = 0; i < 1000; i++) document.write('Lorem Ipsum ');</script>
  </body>
</html>

If you try this, you’ll see you can do mshta c:pathtofile.hta third to jump to the third header for example.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement