原生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;
}
//檢測 Flash
if(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;
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM