简体   繁体   English

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

[英]How to run .exe file or .bat file based on button click event using Javascript

In my current project, I would like to run .bat or .exe file using button click event using JavaScript.在我当前的项目中,我想使用 JavaScript 使用按钮单击事件运行 .bat 或 .exe 文件。 The content of batch file is as shown below:批处理文件的内容如下所示:

start "S:\" TemperatureSensor.exe

which start TemperatureSensor.exe file when TemperatureSensor button is clicked.单击TemperatureSensor按钮时启动TemperatureSensor.exe文件。 Code for HTML page is shown below: 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>

When I clicked on Temperature Sensor button, it should run Test.bat file but it just display following in new page:当我点击温度传感器按钮时,它应该运行 Test.bat 文件,但它只是在新页面中显示以下内容:

在此处输入图片说明

Am I missing ??我失踪了吗?? Is it possible to run .exe file using button click event??是否可以使用按钮单击事件运行 .exe 文件?

Updated: Code for HTML page is shown below:更新: 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>

When I clicked on Temperature Sensor button it displays error: Uncaught ReferenceError: ActiveXObject is not defined .当我单击温度传感器按钮时,它显示错误:未捕获的 ReferenceError: ActiveXObject is not defined

Just save this code as RunExe.hta and not RunExe.html and executes it by double click on it !只需将此代码保存为RunExe.hta而不是RunExe.html并通过双击它来执行它!

EDIT : REMARK about (HTA) (HTML Application)编辑:关于 (HTA) (HTML 应用程序) 的评论

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 应用程序 (HTA)是一种 Microsoft Windows 程序,其源代码由 HTML、动态 HTML 和 Internet Explorer 支持的一种或多种脚本语言(如 VBScript 或 JScript)组成。

The HTML is used to generate the user interface, and the scripting language is used for the program logic. HTML 用于生成用户界面,脚本语言用于程序逻辑。

A HTA executes without the constraints of the internet browser security model; HTA 的执行不受 Internet 浏览器安全模型的限制; in fact, it executes as a "fully trusted" application.事实上,它作为一个“完全受信任的”应用程序执行。

further reading about HTA HTML 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>

If you're using firefox you could use this addon to open batch or exe files.如果您使用 firefox,您可以使用此插件打开批处理或 exe 文件。

Exe and batch files aren't opening in browser by default becaue of security restrictions.由于安全限制,默认情况下不会在浏览器中打开 exe 和批处理文件。

In a later version of the addon there will be an enable preference for exe-files and these files will be disabled by default.在插件的更高版本中,将有一个启用 exe 文件的首选项,默认情况下将禁用这些文件。

But at the moment you can create links like <a href="file://c:/test.bat">test</a> and launch the file with a click.但目前您可以创建像<a href="file://c:/test.bat">test</a>这样的链接,然后单击启动文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM