在Firefox中运行Exe为什么会出现错误 [英] Running Exe in Firefox why do I get an error

查看:92
本文介绍了在Firefox中运行Exe为什么会出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Firefox中运行此程序,单击链接时,Firefox说NS_ERROR_FILE_UNRECOGNIZED_PATH,其中我遵循了此处的说明

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>

推荐答案

在javascript文字中,反斜杠表示转义序列的开始.如果您确实想表示一个反斜杠,则可以使用一个双反斜杠对其进行转义.

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启动winword.exe'

ie 'C:\\Windows\\System32\\cmd.exe /c start winword.exe'

http://www.javascriptkit.com/jsref/escapesequence.shtml

从您链接的帖子中对正确答案的评论看来,他的工作方式似乎是:

From the comments on the correct answer from the post you linked, it looks like the way he got it working was:

仅将路径传递给runexe: javascript:RunExe('C:\ Windows \ System32 \ cmd.exe')

only pass the path to runexe: javascript:RunExe('C:\Windows\System32\cmd.exe')

将参数设置为等于命令args: var parameters = ["/c start winword.exe"];

set the params equal to the command args: var parameters = ["/c start winword.exe"];

所以这在理论上是可行的:

So this would work theoretically:

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

尽管很明显,将参数作为参数传递比像我在此处进行硬编码(或将其作为路径的一部分进行解析并解析出来)要好

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)

这篇关于在Firefox中运行Exe为什么会出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆