Reputation: 85
I want to execute a batch file on a button click event from simple HTML page.
In IE, I can use ActiveXObject
to achieve this however in other browser ActiveXObject
is not supported.
Sample HTML File:
<html>
<head>
<title>Run Batch</title>
<HTA:APPLICATION
APPLICATIONNAME="Run Batch"
ID="MyHTMLapplication"
VERSION="1.0"/>
</head>
<script type="text/javascript">
function RunBat(){
var shell = new ActiveXObject("WScript.Shell");
var path = "D:/Test.bat";
shell.run(path);
}
</script>
</head>
<form>
Execute:
<input type="button" Value="Run" onClick="RunBat();"/>
</form>
</html>
I have gone through many questions on different forums and what I have found is that, in other browsers it is possible through some add-ons.
Is there any other way to execute it without using any add-ons in other browser?
If no, what are the add-ons I can use for Firefox, Chrome and Edge browsers to achieve this?
Upvotes: 2
Views: 1553
Reputation: 25
Due to security reasons it's not possible to launch user files (as batch scripts) from the web browser. This is unless you're trying to develope an electron app, which i think you could see, in that case try this code: (REQUIRES NODE.JS INTEGRATION)
"use strict";
var myBatFilePath = "C:\\Path\\To\\User\\s\\file.bat";
const spawn = require('child_process').spawn;
var bat = spawn('cmd.exe', ['/c', myBatFilePath]);
bat.stdout.on('data', (data) => {
//Logs the batch's echos to the console
var str = String.fromCharCode.apply(null, data);
console.info(str);
});
bat.stderr.on('data', (data) => {
//Logs as error the batch's echos that end with "1>&2"
var str = String.fromCharCode.apply(null, data);
console.error(str);
});
bat.on('exit', (code) => {
//Handles batch exit codes
var preText = `Child exited with code ${code} : `;
switch(code){
case 0:
console.info(preText); // EXIT CODE 0 (no exit code provided)
break;
case 1:
console.info(preText); // EXIT CODE 1
break;
case 2:
console.info(preText); // EXIT CODE 2
break;
case 3:
console.info(preText); // EXIT CODE 3
break;
//AND SO ON
}
});
Upvotes: 1