我有一个内部网 Web 应用程序需要运行一些外部应用程序,如 Word、记事本和其他特定应用程序...我的代码允许使用 IE (ActiveX) 和 Firefox (XPCOM) 进行访问。当我使用整个路径(如“C:\windows\notepad.exe”)时,我可以在两种浏览器中运行,但问题是:某些应用程序有很多版本,如 Microsoft Word(2003、2007、2010.. .),并且本地路径总是不同的,但是如果我在 Windows 中使用“运行...”选项,我只能键入“winword.exe”并加载 MS Word,除了它的版本。如果我只将文件名传递给 IE 中的 ActiveX,我可以调用 MS Word,但在 Firefox 中,使用 XPCOM,我不能。所以,我的问题是:有什么方法可以让 XPCOM 代码仅使用它的相对路径(文件名)运行 MS Word?我已经测试了很多方法但没有成功。
这是我的代码:
function RunExe(path) {
try {
var ua = navigator.userAgent.toLowerCase();
if (ua.indexOf("msie") != -1) {
MyObject = new ActiveXObject("WScript.Shell")
MyObject.Run(path);
} else {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var exe = window.Components.classes['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile);
exe.initWithPath(path);
var run = window.Components.classes['@mozilla.org/process/util;1'].createInstance(Components.interfaces.nsIProcess);
run.init(exe);
var parameters = [""];
run.run(false, parameters, parameters.length);
}
} catch (ex) {
alert(ex.toString());
}
}
调用是这样的:
<a href="#" onclick="javascript:RunExe('winword.exe');">Open Word</a>
如有任何帮助,我们将不胜感激。谢谢。
最佳答案
我认为您的问题在于 IE 直接与 Windows 一起使用,而 Firefox 旨在跨平台。假设您只希望它在 Windows 上运行,您可以执行命令提示符
C:\Windows\System32\cmd.exe
并传递一个参数给它
start winword.exe
然后它将以与 Run 相同的方式执行。
关于javascript - 如何使用 Javascript/XPCOM 打开 .EXE 作为 Windows "Run..."?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6177583/