在我当前的项目中,我想使用 JavaScript 使用按钮单击事件来运行 .bat 或 .exe 文件。批处理文件内容如下:
start "S:\" TemperatureSensor.exe
点击 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>
当我点击温度传感器按钮时,它应该运行 Test.bat 文件,但它只在新页面中显示以下内容:
我是不是不见了??是否可以使用按钮点击事件来运行.exe文件??
更新: 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>
当我点击温度传感器按钮时,它显示错误:Uncaught ReferenceError: ActiveXObject is not defined。
最佳答案
只需将此代码保存为 RunExe.hta 而不是 RunExe.html 并双击执行就可以了!
编辑:关于 (HTA)(HTML 应用程序)的备注
HTML 应用程序 (HTA) 是一种 Microsoft Windows 程序,其源代码由 HTML、动态 HTML 以及 Internet Explorer 支持的一种或多种脚本语言组成,例如VBScript 或 JScript。
HTML 用于生成用户界面,脚本语言用于程序逻辑。
HTA 的执行不受互联网浏览器安全模型的约束;事实上,它作为“完全受信任”的应用程序执行。
进一步阅读 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>
关于javascript - 如何使用 Javascript 基于按钮单击事件运行 .exe 文件或 .bat 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36104713/