場景:用js執行本地的安裝軟件,如果不存在就執行安裝
操作步驟:
1.前台js代碼
<script type="text/javascript"> function exec () { var command; var shell = new ActiveXObject("WScript.Shell"); try { command = shell.RegRead("HKEY_LOCAL_MACHINE\\SOFTWARE\\Classes\\Directory\\shell\\RadiAnt\\Icon");//從注冊表獲得軟件安裝路徑 } catch(e) { if(window.confirm("你可能還沒有安裝Radiant,請下載安裝")){ //alert("確定"); window.location.href="jspView/radiantsetup32312921.exe"; return false; }else{ return false; } }//執行exe程序 window.oldOnError = window.onerror; window._command = command; window.onerror = function (err) { if (err.indexOf('utomation') != -1) { alert('命令' + window._command + ' 已經被用戶禁止!'); return true; } else return false; }; var wsh = new ActiveXObject('WScript.Shell'); if (wsh) wsh.Run("\""+command); //解決目錄包含空格問題 window.onerror = window.oldOnError; } </script>
執行exec()就能調用本地軟件了
以下是需要注意的幾點
1.該程序只能在IE上執行
2.注冊表路徑的查找步驟
右鍵-復制項名稱
3.想要執行改程序必須更改本地的瀏覽器安全級別
否則報錯:ERROR:Automation 服務器不能創建對象
具體參考百度文檔:http://jingyan.baidu.com/article/ae97a646d43dc8bbfd461d0b.html