如何使用Javascript-复制基于按钮单击事件运行.exe文件或.bat文件

Sau*_*han 3 javascript batch-file

在我当前的项目中,我想使用JavaScript使用按钮单击事件运行.bat或.exe文件.批处理文件的内容如下所示:

start "S:\" TemperatureSensor.exe
Run Code Online (Sandbox Code Playgroud)

单击TemperatureSensor按钮时启动TemperatureSensor.exe文件.HTML页面的代码如下所示:

<!DOCTYPE html>
<html>
<body>

<p>Click the button to make a BUTTON element with text.</p>

<button onclick="window.open('file:///S:/Test.bat')">Temperature Sensor</button>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

当我点击温度传感器按钮时,它应该运行Test.bat文件,但它只显示在新页面中:

在此输入图像描述

我错过了吗?是否可以使用按钮单击事件运行.exe文件?

更新: HTML页面的代码如下所示:

<!DOCTYPE html>
<html>
<body>

<p>Click the button to make a BUTTON element with text.</p>

<button onclick="myFunction()">Temperature Sensor</button>

<script>
function myFunction() {
      var oShell = new ActiveXObject("Shell.Application");

var commandtoRun = "C:\\TemperatureSensor.exe";
if (inputparms != "") {
var commandParms = document.Form1.filename.value;
 }

 // Invoke the execute method.  
 oShell.ShellExecute(commandtoRun, commandParms, "", "open", "1");
 }
 </script>

 </body>
 </html>
Run Code Online (Sandbox Code Playgroud)

当我单击"温度传感器"按钮时,它显示错误:未捕获的ReferenceError:未定义ActiveXObject.

Hac*_*koo 12

只需将此代码保存为RunExe.hta而不是RunExe.html,然后双击它即可执行它!

<html>
<head>
<title>Run Exe or Bat files from HTA by Hackoo</title>
<HTA:APPLICATION
  APPLICATIONNAME="Run Exe or Bat files from HTA by Hackoo"
  ID="MyHTMLapplication"
  VERSION="1.0"/>
</head>
<script>
function RunExe(){
    var shell = new ActiveXObject("WScript.Shell");
    var path = '"S:/Test.bat"';
    shell.run(path,1,false);
}
</script>
<input style="width: 170px; height:23px; color: white; background-color: #203040; 
font-family:Book Antiqua;" type="button" Value="Temperature Sensor" onClick="RunExe();"
</html>
Run Code Online (Sandbox Code Playgroud)