![](/img/trans.png)
[英]How to run bat file from HTML Page on button click using javascript in latest Chrome/Firefox?
[英]How to run .exe file or .bat file based on button click event using Javascript
在我當前的項目中,我想使用 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>
當我單擊溫度傳感器按鈕時,它顯示錯誤:未捕獲的 ReferenceError: ActiveXObject is not defined 。
只需將此代碼保存為RunExe.hta而不是RunExe.html並通過雙擊它來執行它!
編輯:關於 (HTA) (HTML 應用程序) 的評論
HTML 應用程序 (HTA)是一種 Microsoft Windows 程序,其源代碼由 HTML、動態 HTML 和 Internet Explorer 支持的一種或多種腳本語言(如 VBScript 或 JScript)組成。
HTML 用於生成用戶界面,腳本語言用於程序邏輯。
HTA 的執行不受 Internet 瀏覽器安全模型的限制; 事實上,它作為一個“完全受信任的”應用程序執行。
進一步閱讀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>
如果您使用 firefox,您可以使用此插件打開批處理或 exe 文件。
由於安全限制,默認情況下不會在瀏覽器中打開 exe 和批處理文件。
在插件的更高版本中,將有一個啟用 exe 文件的首選項,默認情況下將禁用這些文件。
但目前您可以創建像<a href="file://c:/test.bat">test</a>
這樣的鏈接,然后單擊啟動文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.