如何使用JavaScript运行.bat文件

4

我知道在JavaScript中运行.bat文件会涉及一些浏览器问题,但是下面的代码在Chrome和IE上都无法工作。

<html>
<head>
<script type="text/javascript">
function runApp(which) {
  WshShell = new ActiveXObject("WScript.Shell");
  WshShell.Run (which,1,true);
}
</script>
</head>
<body>
<!-- Two ways to create a link to run the app. -->
<font onClick="runApp('file:C:/path/to/batfile.bat');" style="cursor: hand;"><u>Notepad</u>  </font>
<br>
<!-- Or use <a> descriptor -->
<a href="runApp('file://c:/test.bat');">Batch File</a>
</body>
</html>

6
在非IE浏览器中无法使用ActiveX组件,因此在Chrome中尝试这样做是不会有成果的。在IE中,必须启用ActiveX。https://dev59.com/4kvSa4cB1Zd3GeqPfY9P - Paul S.
7
哇,一个<font>标签...不管你从哪里得到这个例子,都要找另一个来源。 - Pointy
对于本地文件,文件URL有三个斜杠。例如:file:/// - mshsayem
3
我能立刻感受到这充满了好意。 - dead beef
以上的解决方案限制了我们只能在IE上运行脚本,因为它使用了ActiveX。那其他浏览器怎么办? - Praveen Pandey
2个回答

2
在IE浏览器上,在标签中的函数runApp(which){}下添加以下函数:
// A fucntion to run any cmd command
function runApp(which) {
    // Using Windows Script Host Shell ActiveX to run cmd commands
    WshShell = new ActiveXObject("WScript.Shell");
    WshShell.Run("cmd /c " + which);
}
// A function to set the Optimal Internet Options
function setOptimalInternetOptionsForDevelopers() {
    // 1- Settings: Don't show the Dialog box "An ActiveX control on this page might be unsafe to interact
    // with other parts of the page. Do you want to allow this interaction?"
    // Refer to https://dev59.com/JEfRa4cB1Zd3GeqP_K-G
    var registryKey = '"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\0"';
    // 1201 Value means: Initialize and script ActiveX controls not marked as safe
    // Refer to http://support.microsoft.com/kb/833633
    var regValName = "1201";
    // Value of 1 will show the Dialog box (Default Value)
    // Value of 0 will not show that dialog box (we need to set this value)
    var regValue = "0";
    runApp('reg add ' + registryKey + ' /v ' + regValName + ' /t REG_DWORD /d ' + regValue + ' /f');

    // 2- Internet Options => Advanced tab => Check "Allow active content to run in files on My Computer*
    // Default (1) not checked
    // we need to change it to (0) to allow this option
    // Refer to http://support.microsoft.com/kb/2002093
    registryKey = '"HKCU\\Software\\Microsoft\\Internet Explorer\\Main\\FeatureControl\\FEATURE_LOCALMACHINE_LOCKDOWN"';
    regValName = "iexplore.exe";
    regValue = "0";
    runApp('reg add ' + registryKey + ' /v ' + regValName + ' /t REG_DWORD /d ' + regValue + ' /f');
}
Now, all you have to do is launch it in the <body> section before any script is needed to be triggered.
For Example:
<body>
    <script type="text/javascript">
        setOptimalInternetOptionsForDevelopers();
        // the rest of the content
    </script>
</body>

注意: 第一次启动此页面时,它会提示一个"允许阻止的内容"的消息,请点击以接受。然后在出现的下一个对话框中单击"是"对话框,该对话框为"此页面上的 ActiveX 控件可能与页面的其他部分不安全进行交互。您要允许此交互吗?" 这仅会发生在您首次启动页面时。下一次,一切都将顺利运行,不再有其他消息。 当然,你可以修改我的函数runApp(which){}到任何其他名称来使用。 虽然上述脚本适用于IE,但我不认为它适用于Chrome或Firefox。

0

我能够在IE浏览器中完成它,而且你的问答帮了我很多。

另外,我想补充说,我能够通过将批处理命令粘贴到runApp函数中来完成所需操作,就像这样

  function runApp() {
               WshShell = new ActiveXObject("WScript.Shell");
              WshShell.Run("explorer.exe \\\\hostname.remote.com"1,true);
               }

额外的反斜杠 '\' 是用于转义的。这很好!下面的问答也鼓励我这样做: VBScript:如何使用参数调用Run()


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接