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

2 回答 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 http://stackoverflow.com/questions/894369/an-activex-control-on-this-page-might-be-unsafe
    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。

于 2014-02-21T16:14:26.943 回答
0

我能够在 IE 资源管理器中做到这一点,并且很大程度上你的 Q/A 帮助了我很多。

另外我想补充一点,我能够使用粘贴在 runApp 函数中的批处理命令来完成所需的操作,如下所示

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

额外的斜杠 '\' 用于转义。这是相当不错的!下面的 Q/A 鼓励我也这样做 VBScript: How to call Run() with parameters

于 2016-12-16T11:11:22.543 回答