如何通过IE中的ActiveX控件(JavaScript)查询系统已安装的所有浏览器
如何借助ActiveX控件查询系统中已安装的所有浏览器?
嘿,看你已经搞定了用ActiveX从IE启动Chrome的操作,现在想扩展到查询系统里所有已安装的浏览器对吧?这事儿得结合Windows系统的特性来搞,毕竟浏览器的安装信息大多存在注册表或者固定路径里,下面给你几个实用的方案:
方案1:通过注册表查询已注册的浏览器
Windows会把默认浏览器以及已注册的浏览器信息存在注册表中,咱们可以用WScript.Shell的RegRead方法来读取这些信息。这里给你一个示例代码,能列出常见的已安装浏览器:
var shell = new ActiveXObject("WScript.Shell"); var browsers = []; // 检查Chrome try { var chromePath = shell.RegRead("HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\chrome.exe\\"); browsers.push("Chrome: " + chromePath); } catch(e) {} // 检查Firefox try { var firefoxPath = shell.RegRead("HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\firefox.exe\\"); browsers.push("Firefox: " + firefoxPath); } catch(e) {} // 检查Edge(Chromium版) try { var edgePath = shell.RegRead("HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\msedge.exe\\"); browsers.push("Edge: " + edgePath); } catch(e) {} // 检查IE(本身就是宿主,不过也可以验证路径) try { var iePath = shell.RegRead("HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\iexplore.exe\\"); browsers.push("Internet Explorer: " + iePath); } catch(e) {} // 输出结果 if (browsers.length > 0) { alert("已安装的浏览器:\n" + browsers.join("\n")); } else { alert("未检测到常见浏览器"); }
方案2:检查常见浏览器的默认安装路径
有些浏览器可能没在注册表的App Paths里注册,这时候可以直接检查它们的默认安装目录是否存在,用Scripting.FileSystemObject来判断文件是否存在:
var fso = new ActiveXObject("Scripting.FileSystemObject"); var browsers = []; // Chrome默认路径(32位和64位) var chromePaths = [ "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe" ]; chromePaths.forEach(function(path) { if (fso.FileExists(path)) { browsers.push("Chrome: " + path); } }); // Firefox默认路径 var firefoxPath = "C:\\Program Files\\Mozilla Firefox\\firefox.exe"; if (fso.FileExists(firefoxPath)) { browsers.push("Firefox: " + firefoxPath); } // Edge默认路径 var edgePath = "C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe"; if (fso.FileExists(edgePath)) { browsers.push("Edge: " + edgePath); } // 输出结果 if (browsers.length > 0) { alert("已安装的浏览器:\n" + browsers.join("\n")); } else { alert("未检测到常见浏览器"); }
注意事项
- ActiveX只能在Internet Explorer浏览器中使用,其他现代浏览器(Chrome、Firefox等)都已经禁用了ActiveX支持。
- 读取注册表或者访问文件系统需要相应的权限,如果用户的IE安全级别设置过高,可能会阻止这些操作。
- 要考虑32位和64位系统的差异,比如Chrome在64位系统中可能安装在
Program Files或Program Files (x86)目录下。
内容的提问来源于stack exchange,提问作者Beginner
相关产品推荐
相关产品推荐

