原生JS检测浏览器安装的插件

navigator里面有plugins 这个属性就是用来检测浏览器插件的。plugins 返回的结果 是一个数组形式。
该数组中的每一项都包含下列属性。
? name :插件的名字。
? description :插件的描述。
? filename :插件的文件名。
? length :插件所处理的 MIME 类型数量

例子如下:

function hasPlugin(name){  name = name.toLowerCase();  for (var i=0; i < navigator.plugins.length; i++){    if (navigator. plugins [i].name.toLowerCase().indexOf(name) > -1){    return true;   }  }  return false;}//检测 Flashif(hasPlugin("Flash")){
  alert(‘你的浏览器有flash插件‘)
};

 IE 不支持 Netscape 式的插件。在 IE 中检测插件的唯一方式就是使用专有的 ActiveXObject 类型

 //检测 IE 中的插件 function hasIEPlugin(name) { try { new ActiveXObject(name); return true; } catch (ex) { return false; } } //检测 Flash alert(hasIEPlugin("ShockwaveFlash.ShockwaveFlash"));

然后封装下就可以了

 function hasPlugins(name) { name = name.toLowerCase(); let result = false; for (var i = 0; i < navigator.plugins.length; i++) { if (navigator.plugins[i].name.toLowerCase().indexOf(name) > -1) { result = true } } if (!result) { try { new ActiveXObject(name); result = true; } catch (ex) { result = false; } } return result; }

 

相关文章