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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36104713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:38:51  来源:igfitidea点击:

How to run .exe file or .bat file based on button click event using Javascript- duplicate

javascriptbatch-file

提问by Saurabh Chauhan

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:

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

start "S:\" TemperatureSensor.exe

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

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

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

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

enter image description here

在此处输入图片说明

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

回答by Hackoo

Just save this code as RunExe.htaand not RunExe.htmland executes it by double click on it !

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

EDIT : REMARK about (HTA) (HTML Application)

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

An 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 用于生成用户界面,脚本语言用于程序逻辑。

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

HTA 的执行不受 Internet 浏览器安全模型的限制;事实上,它作为一个“完全受信任的”应用程序执行。

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>

回答by AWolf

If you're using firefox you could use this addonto 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>,然后单击启动文件。