在工作中,有些需求要打開本地的exe文件,那么javaScript如何實現呢?
本文提供了兩種方法,看看哪種更適合自己
第一種方法:
1.新建一個txt文件,在文件中寫入一下內容,寫完之后保存,然后文件后綴名改為.reg的文件
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Webshell] @="URL:Webshell Protocol Handler" "URL Protocol"="" [HKEY_CLASSES_ROOT\Webshell\DefaultIcon] @="D:\\med_gryl\\sys_main.exe" // 這個需要改成exe文件的路徑 [HKEY_CLASSES_ROOT\Webshell\shell] [HKEY_CLASSES_ROOT\Webshell\shell\open] [HKEY_CLASSES_ROOT\Webshell\shell\open\command] @="D:\\med_gryl\\sys_main.exe" // 這個需要改成exe文件的路徑
2.在html中使用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <a href="Webshell://hello">打開exe文件</a> </body> </html>
第二種方法:
這個方法只有ie瀏覽器支持,所以~~~都懂,廢話不多說,直接上代碼
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <script> function openreg(){ var obj = new ActiveXObject("WScript.Shell"); try{ var s = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\CMS"; //這個路徑一定要是\\雙斜杠 var sNic = obj.RegRead(s) if(sNic!=null){ obj.run("D:\\med_gryl\\sys_main.exe"); //這個路徑一定要是\\雙斜杠 } }catch(e){ alert("本機沒有安裝客戶端,請先下載安裝!") window.open('') } } </script> <h1>通過瀏覽器打開exe文件</h1> <p> <input type="button" value="進入系統" onclick="openreg()"> </p> </body> </html>
ActiveXObject這個方法是ie獨有的,其他瀏覽器不支持