js浏览器和浏览器插件检测的方法总结

本例中机智词ciku.txt放在C盘根目录下,采纳的ActiveXObject插件获取当羊眼半夏件内容。使用此插件不需网络下插件,直接用如下js代码就可以。

复制代码 代码如下:

文/玄魂

前言

第叁遍面临难点所示的须求的时候,头脑中从未别的概念,于是搜索,全数的粤语结果都不是很好听。所幸老外的几篇随笔仍旧很有参谋价值,尽管最后并未有缓慢解决自个儿的主题素材,然则本人可能把这几篇小说的剧情作了抽出和整合治理,于是有了本文。首要内容如下:

1.1  浏览器检查测量检验

1.2  浏览器插件检查实验

1.1  浏览器检测

 

(参考:

对于浏览器的类型和版本的检查评定,常常大家应用navigator.userAgent属性,不过它不能够适用于具备的浏览器,一时还亟需选取navigator.vendor、window.opera只怕navigator.appVersion属性。那四个属性这里不做详细的验证,我们先看上面包车型地铁生机勃勃段代码:

var BrowserDetect = {

    init: function () {

           this.browser =
this.searchString(this.dataBrowser) || “An unknown browser”;

           this.version =
this.searchVersion(navigator.userAgent)

                  ||
this.searchVersion(navigator.appVersion)

                  || “an unknown version”;

           this.OS = this.searchString(this.dataOS)
|| “an unknown OS”;

    },

    searchString: function (data) {

           for (var i=0;i<data.length;i++)  
{

                  var dataString =
data[i].string;

                  var dataProp =
data[i].prop;

                  this.versionSearchString =
data[i].versionSearch || data[i].identity;

                  if (dataString) {

                        if
(dataString.indexOf(data[i].subString) != -1)

                               return
data[i].identity;

                  }

                  else if (dataProp)

                        return
data[i].identity;

           }

    },

    searchVersion: function (dataString) {

           var index =
dataString.indexOf(this.versionSearchString);

           if (index == -1) return;

           return
parseFloat(dataString.substring(index+this.versionSearchString.length+1));

    },

    dataBrowser: [{

                  string: navigator.userAgent,

                  subString: “Chrome”,

                  identity: “Chrome”

           },

{

                  prop: window.opera,

                  identity: “Opera”,

                  versionSearch: “Version”

           },

(略。。。)

]

};

名称叫dataBrowser的数组是我们需求检查测验的浏览器对象,每一种对象包罗三个属性用来支持代码从navigator.userAgent或许window.opera或然navigator.vendor属性中提前浏览器连串和版本新闻。每种对象包罗的习性表达如下:

1)         ``stringsubString``。告诉程序在string属性中搜索subString的内容,从而确定浏览器类型。

2卡塔尔         prop。使用window.opera来判别浏览器类型。

3)         ``identity``。浏览器类型标识字符串。

4)        
versionSearch``。用来检测浏览器版本的值,如果没有该值,使用identity属性替代。

总体代码如下(越来越多解释内容,可参看笔者付出的保加奇瓦瓦语链接):

var BrowserDetect = {

       init: function () {

              this.browser =
this.searchString(this.dataBrowser) || “An unknown browser”;

              this.version =
this.searchVersion(navigator.userAgent)

                    ||
this.searchVersion(navigator.appVersion)

                    || “an unknown version”;

              this.OS =
this.searchString(this.dataOS) || “an unknown OS”;

       },

       searchString: function (data) {

www.hj8828.com,              for (var i=0;i<data.length;i++)  
{

                    var dataString =
data[i].string;

                    var dataProp =
data[i].prop;

                    this.versionSearchString =
data[i].versionSearch || data[i].identity;

                    if (dataString) {

                           if
(dataString.indexOf(data[i].subString) != -1)

                                  return
data[i].identity;

                    }

                    else if (dataProp)

                           return
data[i].identity;

              }

       },

       searchVersion: function (dataString) {

              var index =
dataString.indexOf(this.versionSearchString);

              if (index == -1) return;

              return
parseFloat(dataString.substring(index+this.versionSearchString.length+1));

       },

       dataBrowser: [

              {

                    string:
navigator.userAgent,

                    subString: “Chrome”,

                    identity: “Chrome”

              },

              {      string:
navigator.userAgent,

                    subString: “OmniWeb”,

                    versionSearch: “OmniWeb/”,

                    identity: “OmniWeb”

              },

              {

                    string: navigator.vendor,

                    subString: “Apple”,

                    identity: “Safari”,

                    versionSearch: “Version”

              },

              {

                     prop: window.opera,

                    identity: “Opera”,

                    versionSearch: “Version”

              },

              {

                    string: navigator.vendor,

                    subString: “iCab”,

                    identity: “iCab”

              },

              {

                    string: navigator.vendor,

                    subString: “KDE”,

                    identity: “Konqueror”

              },

              {

                    string:
navigator.userAgent,

                    subString: “Firefox”,

                    identity: “Firefox”

              },

              {

                    string: navigator.vendor,

                    subString: “Camino”,

                    identity: “Camino”

              },

              {             // for newer Netscapes
(6+)

                    string:
navigator.userAgent,

                    subString: “Netscape”,

                    identity: “Netscape”

              },

              {

                    string:
navigator.userAgent,

                    subString: “MSIE”,

                    identity: “Explorer”,

                    versionSearch: “MSIE”

              },

              {

                    string:
navigator.userAgent,

                    subString: “Gecko”,

                    identity: “Mozilla”,

                    versionSearch: “rv”

              },

              {             // for older Netscapes
(4-)

                    string:
navigator.userAgent,

                    subString: “Mozilla”,

                    identity: “Netscape”,

                    versionSearch: “Mozilla”

              }

       ],

       dataOS : [

              {

                    string:
navigator.platform,

                    subString: “Win”,

                    identity: “Windows”

              },

              {

                    string:
navigator.platform,

                    subString: “Mac”,

                    identity: “Mac”

              },

              {

                       string:
navigator.userAgent,

                       subString: “iPhone”,

                       identity: “iPhone/iPod”

           },

              {

                    string:
navigator.platform,

                    subString: “Linux”,

                    identity: “Linux”

              }

       ]

 

};

BrowserDetect.init();

 

1.2  浏览器插件检测

尤为重要参照小说:

对此浏览器插件的检查测试,情状要轻微复杂,包蕴插件是或不是安装和插件版本的检查实验,本文只谈谈IE、Firefox和Chrome浏览器。在IE浏览器中,插件是用作ActiveXObject存在的,必需创制ActiveXObject对象能力检查测量试验插件是不是安装;在Firefox和Chrome中,能够应用navigator.plugins属性来枚举安装的插件。对于插件的版本检查测量检验,在IE中,能够利用相应的ActiveXObject对象的GetVersions(卡塔尔国方法来收获版本消息;在Firefox中,navigator.plugins[‘插件标记’].version的点子来收获版本;在Chrome中,却从不统黄金时代的主意来获得插件版本,有的插件根本超级小概获取版本新闻。

上边是获得浏览器Pdf插件版本音信的代码:

var PdfDetect = {

    GetInfo: function (explorerName) {

        this.pdfVersion = this.searchPdf(explorerName);

    },

    searchPdf: function (explorerName) {

        explorerName = explorerName.toLowerCase();

        if (explorerName != ‘explorer’ && navigator.plugins.length > 0) {

            var version = 0;

            if (navigator.plugins[‘Adobe Acrobat’] || navigator.plugins[‘Adobe PDF Plug-in’]) {

                if (explorerName == ‘chrome’) {

                    //  version = navigator.plugins[i].name.match(/([\d.]+)/) + “<br />”

                    version = 8; //chrome浏览器不只怕有效检查实验pdf版本

                }

                else {

                    version = navigator.plugins[‘Adobe PDF Plug-in’] ? 

                    navigator.plugins[‘Adobe Acrobat’].version : 

                    navigator.plugins[‘Adobe Acrobat’].version;

                }

            }

            return version;

        }

        else {

            var contol = null;

            try {

 

                // AcroPDF.PDF is used by version 7 and later

                control = new ActiveXObject(‘AcroPDF.PDF’);

            } catch (e) {

                return 0;

            }

            if (!control) {

                try {

                    // PDF.PdfCtrl is used by version 6 and earlier

                    control = new ActiveXObject(‘PDF.PdfCtrl’);

                } catch (e) {

                    return 0;

                }

            }

            if (control) {

                isInstalled = true;

                version = control.GetVersions().split(‘,’);

                version = version[0].split(‘=’);

                version = parseFloat(version[1]);

                return version;

            }

            return 0;

        }

    }

在上头的代码中,大家能够见见,在IE6和先前时代版本的浏览器中应用control = new
ActiveXObject(‘PDF.PdfCtrl’)来成立Pdf插件对象,在修正版本的IE浏览器中采取control = new ActiveXObject(‘AcroPDF.PDF’卡塔尔国来成立Pdf对象,然后使用

version = control.GetVersions().split(‘,’);

                version = version[0].split(‘=’);

                version = parseFloat(version[1]);

来获取Pdf版本。在Firefox和Chrome中,使用navigator.plugins[‘Adobe Acrobat’] || navigator.plugins[‘Adobe PDF Plug-in’]来判断是不是安装了Pdf插件。在Firefox中,使用

version = navigator.plugins[‘Adobe PDF Plug-in’] ? 

                    navigator.plugins[‘Adobe Acrobat’].version : 

                    navigator.plugins[‘Adobe Acrobat’].version;

来检查评定Pdf插件的版本消息。

在chrome中,笔者向来不检验Pdf插件的版本。

检查实验Flash
Player插件的代码如下:

var isInstalled = false;
var version = null;
if (window.ActiveXObject) {
var control = null;
try {
control = new ActiveXObject(‘ShockwaveFlash.ShockwaveFlash’);
} catch (e) {
return;
}
if (control) {
isInstalled = true;
version = control.GetVariable(‘$version’).substring(4);
version = version.split(‘,’);
version = parseFloat(version[0] + ‘.’ + version[1]);
}
} else {
//在Firefox和Chrome中使用navigator.plugins[“Shockwave Flash”]检测//Flash Player。}

其余插件的检验,参照他事他说加以考查作者付出的越南语链接。

 

浏览器需改善interner安全选项的品级,启用ActiveX技艺得到到代码中的ActiveXObject插件。如下图所示:

<script type=”text/javascript”>
var xmlHttp;

 // -------------- &#20840;&#23616;&#21464;&#37327;&#65292;&#29992;&#26469;&#21028;&#26029;&#25991;&#26412;&#22495;&#20013;&#26159;&#21542;&#21253;&#21547;&#33039;&#35789;,&#40664;&#35748;&#20026;false,&#21363;&#19981;&#21253;&#21547;&#33039;&#35789;------- var isDirty = false; //&#20351;&#29992;ActiveX&#35835;&#21462;&#26412;&#22320;&#25991;&#20214;&#33719;&#21462;dirtyword&#35789;&#24211; function readFile(){ //var ForReading = 1; var fso = new ActiveXObject("Scripting.FileSystemObject"); openF = fso.OpenTextFile; var cikuStr= openF.ReadAll(); return cikuStr; } /* * &#25552;&#20132;&#34920;&#21333;&#30340;&#20027;&#26041;&#27861; * &#22312;&#25552;&#20132;&#34920;&#21333;&#30340;&#26102;&#20505;&#23545;&#20869;&#23481;&#36827;&#34892;&#36807;&#28388;&#24182;&#22312;&#25991;&#26412;&#22495;&#26174;&#31034;&#36807;&#28388;&#21518;&#30340;&#20869;&#23481; */ function submitForm1() { var messageValue=document.getElementById.value; var cikuStr=readFile(); var cikuArr= new Array(); //&#23450;&#20041;&#25968;&#32452;,&#23384;&#20648;&#25935;&#24863;&#35789; cikuArr=cikuStr.split; //&#25935;&#24863;&#23383;&#31526;&#20998;&#21106; for (var i=0;i&lt;cikuArr.length;i++){ var flag=cikuArr[i]; if(messageValue.indexOf{ //&#26597;&#25214;&#25991;&#26412;&#22495;&#20013;&#26159;&#21542;&#21253;&#21547;&#25935;&#24863;&#23383;&#31526;&#65292;&#26159;&#21017;&#26367;&#25442; filterWord; var ifs=confirm("&#20320;&#30340;&#30041;&#35328;&#20013;&#21547;&#26377;&#19981;&#24688;&#24403;&#30340;&#35789;&#35821;,&#31995;&#32479;&#24050;&#33258;&#21160;&#20026;&#20320;&#20462;&#25913;,&#26159;&#21542;&#32487;&#32493;&#25552;&#20132;&amp;#63;"); break; }else{ //&#26080;&#25935;&#24863;&#23383;&#31526;&#65292;&#30452;&#25509;&#25552;&#20132;&#34920;&#21333; document.getElementById.submit{ //&#29992;&#25143;&#28857;&#20987;&#30830;&#23450;&#65292;&#21017;&#25552;&#20132;&#34920;&#21333; document.getElementById.submit(); } } /* * &#23545;&#20256;&#36827;&#26469;&#30340;messageValue&#36807;&#28388;&#24182;&#36820;&#22238;&#26032;&#20869;&#23481; */ function filterWord{ // &#26681;&#25454;&#25991;&#26412;&#22495;&#30340;id&#33719;&#21462;&#25991;&#26412;&#22495;&#23545;&#35937;&#20869;&#23481; var cikuStr=readFile(); var cikuArr= new Array(); //&#23450;&#20041;&#25968;&#32452;,&#23384;&#20648;&#25935;&#24863;&#35789; cikuArr=cikuStr.split; //&#25935;&#24863;&#23383;&#31526;&#20998;&#21106;&#21040;&#25968;&#32452;&#20869; for (var i=0;i&lt;cikuArr.length;i++){ messageValue=filterOneWord(messageValue,cikuArr[i]);//filterOneWord&#20989;&#25968;&#27599;&#27425;&#26367;&#25442;&#19968;&#20010;&#23383;&#31526;&#65292;&#38656;&#24490;&#29615;&#35843;&#29992; } document.getElementById.value=messageValue; //&#23558;&#26367;&#25442;&#21518;&#30340;&#20869;&#23481;&#26174;&#31034;&#21040;&#25991;&#26412;&#22495;&#20013; } /* * &#36825;&#20010;&#20989;&#25968;&#29992;&#26469;&#36807;&#28388;&#21333;&#20010;&#35789;&#35821;, &#22914;&#26524;messageValue&#20013;&#21547;&#26377;oneDirtyWord, &#21017;&#29992;"**"&#26367;&#25442;&#36825;&#20010;oneDirtyWord * messageValue --- &#35201;&#36807;&#28388;&#30340;&#35821;&#21477; */ function filterOneWord(messageValue,oneDirtyWord){ var str=messageValue.replace(new RegExp,"**"); return str; } 

function creatXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject(“Microsoft.XMLHttp”);
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}