Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. The content of batch file is as shown below:

start "S:\" TemperatureSensor.exe

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>

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

enter image description here

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

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>

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

like image 252
Saurabh Chauhan Avatar asked Dec 08 '22 23:12

Saurabh Chauhan


2 Answers

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

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

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

further reading about 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>
like image 185
Hackoo Avatar answered Jan 14 '23 09:01

Hackoo


If you're using firefox you could use this addon to open batch or exe files.

Exe and batch files aren't opening in browser by default becaue of security restrictions.

In a later version of the addon there will be an enable preference for exe-files and these files will be disabled by default.

But at the moment you can create links like <a href="file://c:/test.bat">test</a> and launch the file with a click.

like image 32
AWolf Avatar answered Jan 14 '23 09:01

AWolf