hta/ javascript How to execute an application with relative path

拥有回忆 提交于 2020-02-25 03:37:01

问题


I'm building a .hta (with javascript) from which i want to launch several applications.

But when i execute my .hta i get the error message can't find file

this is the code:

<script type="text/javascript" language="javascript">
    function RunFile(path) {
    var relpath = window.location.href;
    var fullpath = relpath + path;

    WshShell = new ActiveXObject("WScript.Shell");
    WshShell.Run(fullpath, 1, false);
    }

    RunFile("\file.exe");
</script>

回答1:


window.location.href includes filename and protocol too. Try this:

var relpath = window.location.pathname.replace(/\\/g,'/').split('/');
relpath.pop();// JScript: relpath.length = relpath.length - 1;
relpath = relpath.join('/') + '/';

Notice use of / instead \, and it's also handy to end up relpath with /, so you don't need to add it to function argument.

EDIT

I'm not sure what you mean with getting location without file, maybe this (citation from Windows Sripting Technologies (unfortunately broken now):

"The CurrentDirectory returns a string that contains the fully qualified path of
the current working directory of the active process."

The active process is for example the running HTA, so this will give the local path of the HTA file (without filename).

currentDirectory is a property of WScript.Shell, so you can use it with WshShell in your code, also to set working directory.



来源:https://stackoverflow.com/questions/13843590/hta-javascript-how-to-execute-an-application-with-relative-path

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