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

查看:65
本文介绍了如何使用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>

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

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 !

关于(HTA)(HTML应用程序)的评论

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天全站免登陆