2012-12-12 39 views
1

我正在构建一个.hta(使用javascript),我想从中启动多个应用程序。hta/javascript如何使用相对路径执行应用程序

但是,当我执行我的名.hta我得到的错误信息无法找到文件

这是代码:

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

回答

2

window.location.href包括文件名和协议了。试试这个:

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

通知使用/代替\,这也是方便落得relpath/,所以你不需要将它添加到函数参数。

编辑

我不知道你的意思越来越位置,而文件,也许这(引文来自Windows Sripting Technologies)是什么:

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

的活动进程是例如运行HTA,所以这会给出HTA文件的本地路径(没有文件名)。

currentDirectoryWScript.Shell的一个属性,所以你可以在你的代码中使用它WshShell,也可以设置工作目录。

+0

有没有办法让文件本身没有位置? – user1644062

+0

呃...我的代码就是这样。或者你的意思是没有'window.location'对象的路径? – Teemu

+0

非常感谢你的代码作品,但是当我将地图名称中的空格替换为_时,我得到相同的错误。 – user1644062