从 Javascript 运行 .exe
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3152482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running .exe from Javascript
提问by Manish
I am trying to run a .exe file from Javascript. This is what I have:
我正在尝试从 Javascript 运行 .exe 文件。这就是我所拥有的:
var oShell = new ActiveXObject("Shell.Application");
var commandtoRun = "C:\Documents and Settings\User\Desktop\ABCD.exe"; oShell.ShellExecute(commandtoRun,"","","open","1");
var oShell = new ActiveXObject("Shell.Application");
var commandtoRun = "C:\Documents and Settings\User\Desktop\ABCD.exe"; oShell.ShellExecute(commandtoRun,"","","open","1");
If I have only the first 2 lines code it seems to work fine (it asked me do I want activeX when I opened it first time in IE) but if I add the last line (ShellExecute) there seems to be an error. I want to pass arguments to the exe.
如果我只有前 2 行代码,它似乎工作正常(当我第一次在 IE 中打开它时它问我是否需要 activeX)但如果我添加最后一行(ShellExecute),似乎有错误。我想将参数传递给 exe。
Does anyone know how to do it ?
有谁知道怎么做?
采纳答案by RedFilter
You need to escape the backslashes, e.g.,
您需要转义反斜杠,例如,
var commandtoRun = "C:\Documents and Settings\User\Desktop\ABCD.exe";
Update:
更新:
This works fine on my machine:
这在我的机器上运行良好:
var oShell = new ActiveXObject("Shell.Application");
var commandtoRun = "C:\Windows\notepad.exe";
oShell.ShellExecute(commandtoRun,"","","open","1");
Update 2
更新 2
You can save this as a file with the extension .htaand it should work in your browser:
您可以将其保存为带有扩展名的文件,.hta它应该可以在您的浏览器中运行:
<HTA:APPLICATION ID="oMyApp"
APPLICATIONNAME="Application Executer"
BORDER="no"
CAPTION="no"
SHOWINTASKBAR="yes"
SINGLEINSTANCE="yes"
SYSMENU="yes"
SCROLL="no"
WINDOWSTATE="normal">
<script type="text/javascript" language="javascript">
var oShell = new ActiveXObject("Shell.Application");
var commandtoRun = "C:\Windows\notepad.exe";
oShell.ShellExecute(commandtoRun,"","","open","1");
</script>

