JavaScript如何打开exe文件

在工作中,有些需求要打开本地的exe文件,那么javaScript如何实现呢?

本文提供了两种方法,看看哪种更适合自己

第一种方法:

1.新建一个txt文件,在文件中写入一下内容,写完之后保存,然后文件后缀名改为.reg的文件

Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOTWebshell]
@="URL:Webshell Protocol Handler"
"URL Protocol"=""
[HKEY_CLASSES_ROOTWebshellDefaultIcon]
@="D:\med_gryl\sys_main.exe"    // 这个需要改成exe文件的路径
[HKEY_CLASSES_ROOTWebshellshell]
[HKEY_CLASSES_ROOTWebshellshellopen]
[HKEY_CLASSES_ROOTWebshellshellopencommand]
@="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独有的,其他浏览器不支持
原文地址:https://www.cnblogs.com/97Coding/p/13539832.html