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

标签 javascript batch-file

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

start "S:\" TemperatureSensor.exe

点击 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>

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

enter image description here

我是不是不见了??是否可以使用按钮点击事件来运行.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>

当我点击温度传感器按钮时,它显示错误:Uncaught ReferenceError: ActiveXObject is not defined

最佳答案

只需将此代码保存为 RunExe.hta 而不是 RunExe.html 并双击执行就可以了!

编辑:关于 (HTA)(HTML 应用程序)的备注

HTML 应用程序 (HTA) 是一种 Microsoft Windows 程序,其源代码由 HTML、动态 HTML 以及 Internet Explorer 支持的一种或多种脚本语言组成,例如VBScript 或 JScript。

HTML 用于生成用户界面,脚本语言用于程序逻辑。

HTA 的执行不受互联网浏览器安全模型的约束;事实上,它作为“完全受信任”的应用程序执行。

进一步阅读 HTA HTML Application

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

关于javascript - 如何使用 Javascript 基于按钮单击事件运行 .exe 文件或 .bat 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36104713/

相关文章:

javascript - 如何将文本区域中每行的字符数限制为固定值

javascript - 来自 $.ajax() 请求的 vue.js 数据未绑定(bind)到表

javascript - 关联数组是否像哈希表一样执行?

javascript - 我想在一定时间后自动淡入 div onclick 和淡出 div

batch-file - 如何让.BAT文件完成后自行删除?

windows - 使用 VBS 解压时出错

javascript - element.css ('left' ) 返回 'auto' 即使它不在 iPhone Safari 上

windows - 如何在cmd中使用通配符重命名目录

batch-file - 命名参数如何传递给批处理文件?

batch-file - 复制文件列表 (txt)