5

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.

6
  • See stackoverflow.com/questions/1880198/… Commented Mar 19, 2016 at 17:54
  • You should use a HTA to bypass the security issue Commented Mar 19, 2016 at 18:00
  • @Hackoo Could you please elaborate in detail ??? Commented Mar 19, 2016 at 18:02
  • @guest271314- I got error Uncaught ReferenceError: ActiveXObject is not defined. What should I do ?? Commented Mar 19, 2016 at 18:11
  • @Saurabh13 Have you tried one of the alternative approaches at linked page ? Commented Mar 19, 2016 at 19:27

2 Answers 2

13

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>
Sign up to request clarification or add additional context in comments.

6 Comments

@Hackoo- Thank you for the solution. Now, Can I run this code in linux ?? If not than what should I do to run such kind of code in linux ??
It work well while I run bat file. However how to run batch file with administrative privileges.
Can I use relative path to bat file? I will give the html to someone else can't be sure where they will put it on their hard drive.
is ActiveXObject only available on IE? can we use ActiveXObject in chrome? How?
@RaviMakwana You can't use chrome to execute an EXE,BAT,VBS 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. The HTML is used to generate the user interface, and the scripting language is used for the program logic.An 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
|
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.