Open a file with an HTA application

做~自己de王妃 提交于 2021-01-29 08:33:50

问题


Here's a code for an HTA file:

<html>
<head>
     <title>HTA application</title>
     <script type="text/javascript">
          function f(x){alert("You opened the file " + x)}
     </script>
</head>
<body>
     Here's some text in an HTA application...
</body>
</html>

Is there a way of associating a file extension with this HTA application on Windows so that when you open a file with this extension it opens the HTA application and does the function f(path) where path is the path of the opened file?


回答1:


No, any extension can't do that. You'll need to call your function somehow. Either using a direct call after declaring the function, like so:

f(window.location.href);

... or replace the declaration with an IIFE (Immediately Invoked Function Expression):

(function f(x){alert("You opened the file " + x)}(window.location.href));

window.location.href contains the protocol and a full path to the file. If you need the path only, use window.location.pathname.




回答2:


You can use GET parameters to pass parameters to an HTA file, for example a file name. To do that, you add a ? after the HTA file's path, and then add whatever parameters you want to pass, for example C:\path\program.hta?parameter.

In order to associate a certain file type with an HTA program, you can associate the file extension just like you would do for an executable, except that instead of opening it with someprogram.exe "%1" you would open it with mshta.exe "C:\path\program.hta?%1". When you open the file, the %1 part will automatically get replaced with the name of the file that's being opened.

Here is how you would associate files with the file extension .test to an HTA file at C:\path\program.hta:

HKEY_CLASSES_ROOT\.test
    examplefile
HKEY_CLASSES_ROOT\examplefile\shell\open\command
    mshta.exe "C:\path\program.hta?%1"

In order to access these parameters in the HTA program using Javascript, location.search.substr(1) returns everything that's after the question mark. So in a simple case where the only parameter that's being passed is the file name, you would have for example:

<html>
<head>
     <title>HTA application</title>
     <script type="text/javascript">
          alert("You opened the file " + location.search.substr(1))
     </script>
</head>
<body>
     Here's some text in an HTA application...
</body>
</html>

You can pass other parameters as well by using delimiters in the parameter string. On the web (as opposed to in HTA programs), the most common delimiter is &, for example C:\path\program.hta?parameter1&parameter2. The problem with using & if your parameters are filenames is that filenames can contain an & character. On the web, this is usually solved by escaping any & characters in the parameter names by %26, but I'm not aware of any way to escape file names like this in the registry, so you need to be careful about this. For HTA files that can take file names as parameters I would recommend using another delimiter that can't be used in file names, for example * or |. On the web, the main reason to stick to the convention and use & as a delimiter is because server side programming languages like PHP recognize & as a delimiter and provide ways to access parameters delimited by & easily, but HTA programs don't have servers so as long as you're using HTA you don't need to worry about what server side languages do.



来源:https://stackoverflow.com/questions/27693382/open-a-file-with-an-hta-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!