如何使用Javascript基于按钮点击事件运行.exe文件或.bat文件 [英] How to run .exe file or .bat file based on button click event using Javascript

查看:36
本文介绍了如何使用Javascript基于按钮点击事件运行.exe文件或.bat文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my current project, I would like to run .bat or .exe file using button click event using JavaScript. The content of batch file is as shown below:

start "S:" TemperatureSensor.exe

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

which start TemperatureSensor.exe file when TemperatureSensor button is clicked. Code for HTML page is shown below:

<!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 文件,但它只是在新页面中显示以下内容:

When I clicked on Temperature Sensor button, it should run Test.bat file but it just display following in new page:

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

Am I missing ?? Is it possible to run .exe file using button click event??

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

Updated: Code for HTML page is shown below:

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

When I clicked on Temperature Sensor button it displays error: Uncaught ReferenceError: ActiveXObject is not defined.

推荐答案

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

Just save this code as RunExe.hta and not RunExe.html and executes it by double click on it !

EDIT : REMARK about (HTA) (HTML Application)

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

HTML Application (HTA) is a Microsoft Windows program whose source code consists of HTML, Dynamic HTML, and one or more scripting languages supported by Internet Explorer, such as VBScript or JScript.

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

The HTML is used to generate the user interface, and the scripting language is used for the program logic.

HTA 的执行不受互联网浏览器安全模型的限制;事实上,它作为一个完全受信任的"执行.申请.

A HTA executes without the constraints of the internet browser security model; in fact, it executes as a "fully trusted" application.

进一步阅读HTA 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>

这篇关于如何使用Javascript基于按钮点击事件运行.exe文件或.bat文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆