我在Firefox中运行此功能,点击链接时,Firefox说NS_ERROR_FILE_UNRECOGNIZED_PATH,我按照这里的说明How to open .EXE with Javascript/XPCOM as Windows "Run..."?
<html>
<head>
<script>
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());
}
}
</script>
</head>
<body>
<a href="#" onclick="javascript:RunExe('C:\Windows\System32\cmd.exe /c start winword.exe');">Open Word</a>
</body>
答案 0 :(得分:2)
在javascript文字中,反斜杠表示转义序列的开头。如果你真的想要表示反斜杠,你可以使用双反斜杠来逃避它。
即 ' C:\\ Windows \\ System32 \\ cmd.exe / c start winword.exe'
http://www.javascriptkit.com/jsref/escapesequence.shtml
编辑: 从您链接的帖子的正确答案的评论中,看起来他的工作方式是:
只将路径传递给runexe: 的的javascript:RunExe( 'C:\ Windows \ System32下\ cmd.exe的')强>
将params设置为等于命令args: var parameters = [“/ c start winword.exe”];
所以这在理论上会起作用:
<html>
<head>
<script>
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 = ["/c start winword.exe"];
run.run(false, parameters, parameters.length);
}
} catch (ex) {
alert(ex.toString());
}
}
</script>
</head>
<body>
<a href="#" onclick="javascript:RunExe('C:\\Windows\\System32\\cmd.exe');">Open Word</a>
</body>
虽然显然最好将params作为参数传递,而不是像我在这里所做的那样硬编码(或者将它们作为路径的一部分传递并解析出来)