Html 如何使用 javascript 运行 .bat 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18027894/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 11:49:16  来源:igfitidea点击:

How to run a .bat file using javascript

javascripthtmlinternet-explorergoogle-chromebatch-file

提问by user2205925

I know there are some browser issues associated with running a bat file from javascript, but the below code doesnt work on chrome nor on ie

我知道有一些与从 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>

回答by Akram Alhinnawi

On IE, add this function under your function runApp(which){} in the tag

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

Note: The first time you launch this page, it will prompt a message to "Allow blocked content", just click on it to accept it. Then click "yes" on the next dialog box that appears 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?" This will happen only the first time you launch the page. Next time, everything will run like a charm with no more messages. Of course you can modify my function runApp(which) {} to any other name for you to work. Although the above script will work for IE, I don't think it'll work for Chrome or Firefox.

注意:第一次启动该页面时,会提示“允许被阻止的内容”,点击接受即可。然后在出现的下一个对话框中单击“是”对话框“此页面上的 ActiveX 控件与页面的其他部分交互可能不安全。是否允许此交互?” 这只会在您第一次启动页面时发生。下一次,一切都会像魅力一样运行,不再有消息。当然,您可以将我的函数 runApp(which) {} 修改为任何其他名称以供您工作。尽管上述脚本适用于 IE,但我认为它不适用于 Chrome 或 Firefox。

回答by Humayun Al Rasheid

I was able to do it in IE explorer and largely your Q/A helped me a lot.

我能够在 IE 资源管理器中做到这一点,很大程度上您的问答对我帮助很大。

Also I would like to add that I was able to do the required with the commands from the batch pasted inside the runApp function like this

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

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

The extra slashes '\' are for escaping . It was quite good ! Below Q/A encouraged me also to do that VBScript: How to call Run() with parameters

额外的斜杠 '\' 用于转义 . 非常好!下面的 Q/A 也鼓励我这样做 VBScript: How to call Run() with parameters