在 Firefox 中运行 Exe 为什么会出现错误
我在 Firefox 中运行这个,当点击链接时,Firefox 说 NS_ERROR_FILE_UNRECOGNIZED_PATH 而我按照这里的说明进行操作 如何使用 Javascript/XPCOM 作为 Windows“运行...”打开 .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 = [""];
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>
I run this in Firefox, when clicking on link, Firefox says NS_ERROR_FILE_UNRECOGNIZED_PATH wheread I followed the instruction from here 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 JavaScript 文本中,反斜杠表示转义序列的开始。如果您确实想表示反斜杠,则可以使用双反斜杠对其进行转义。
IE
'C:\\Windows\\System32\\cmd.exe /c start winword.exe'
http://www.javascriptkit.com/jsref/escapesequence.shtml
编辑:
从您链接的帖子中对正确答案的评论来看,他的工作方式似乎是:
仅将路径传递给 runexe:
javascript:RunExe('C:\Windows\System32\cmd.exe')
将参数设置为等于命令参数:
varparameters = ["/c start winword.exe"];
所以这在理论上是可行的:
尽管显然将参数作为参数传递比像我在这里所做的那样硬编码它们更好(或者将它们作为路径的一部分传递并解析它们)
In javascript literals, a backslash indicates the beginning of an escape sequence. If you actually want to represent a backslash, you can escape it with a double backslash.
ie
'C:\\Windows\\System32\\cmd.exe /c start winword.exe'
http://www.javascriptkit.com/jsref/escapesequence.shtml
EDIT:
From the comments on the correct answer from the post you linked, it looks like the way he got it working was:
only pass the path to runexe:
javascript:RunExe('C:\Windows\System32\cmd.exe')
set the params equal to the command args:
var parameters = ["/c start winword.exe"];
So this would work theoretically:
Although clearly it would be better to pass in the params as an argument than hardcode them as I've done here (or pass them in as part of the path and parse them out)