Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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.

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

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:

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

<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> 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:\path\to\file.hta third to jump to the third header for example.

This work perfect, thanks! Just notice: This does not strip (possibly present) doublequotes from parameter what is standard way how windows handle parameters and someone could be confused. – user2956477 Jun 4, 2022 at 9:42 It's up to the person passing the parameter whether to put quotes around them or not, since unlike Linux, Windows doesn't have a concept of an arguments array on an OS level but just a command line string. If you wanted to allow the quotes around the anchor name, you could just remove them with .replace(/"/g, '') before using. – CherryDT Jun 4, 2022 at 9:44 there could be a need of doublequotes, for example if parameter contain reserved characters or spaces – user2956477 Jun 4, 2022 at 9:47 Having an URL hash with such characters would be problematic anyway, wouldn't it? And even if that were the case, you wouldn't have to quote it because you are looking at the argument string as a whole here anyway. Anyway, if this is a concern, you can use a function like this to parse the string. I just didn't add it in the answer (only noted that quotes are not taken care of) because I thought it would inflate the answer for something that doesn't even match your usecase. – CherryDT Jun 4, 2022 at 9:54 I do not want to push you to include qoute stripping mechanism, just notice the fact stripping it not happens here, for people from windows world where this is almost natural behaviour. – user2956477 Jun 4, 2022 at 10:43

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.