美文网首页
js判断浏览器类型

js判断浏览器类型

作者: 你这个锤子 | 来源:发表于2020-11-16 16:16 被阅读0次

IE 浏览器通过 window.ActiveXObject 来判断

  • 只有IE支持创建ActiveX控件,因此她有一个其他浏览器没有的东西,就是ActiveXObject函数。只要判断window对象存在ActiveXObject函数,就可以明确判断出当前浏览器是IE。而IE各个版本典型的userAgent如下:
    Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)
    Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2)
    Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
    Mozilla/4.0 (compatible; MSIE 5.0; Windows NT)
    其中,版本号是MSIE之后的数字。

Firefox

  • Firefox中的DOM元素都有一个getBoxObjectFor函数,用来获取该DOM元素的位置和大小(IE对应的中是getBoundingClientRect函数)。这是Firefox独有的,判断它即可知道是当前浏览器是Firefox。Firefox几个版本的userAgent大致如下:
    Mozilla/5.0 (Windows; U; Windows NT 5.2) Gecko/2008070208 Firefox/3.0.1
    Mozilla/5.0 (Windows; U; Windows NT 5.1) Gecko/20070309 Firefox/2.0.0.3
    Mozilla/5.0 (Windows; U; Windows NT 5.1) Gecko/20070803 Firefox/1.5.0.12
    其中,版本号是Firefox之后的数字。

Chrome

  • Chrome有一个MessageEvent函数,但Firefox也有。不过,好在Chrome并没有Firefox的getBoxObjectFor函数,根据这个条件还是可以准确判断出Chrome浏览器的。目前,Chrome的userAgent是:
    Mozilla/5.0 (Windows; U; Windows NT 5.2) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13
    其中,版本号在Chrome只后的数字。有趣的是,Chrome的userAgent还包含了Safari的特征,也许这就是Chrome可以运行所有Apple浏览器应用的基础吧。

Safari

  • Safari浏览器中有一个其他浏览器没有的openDatabase函数,可做为判断Safari的标志。Safari典型的userAgent如下:
    Mozilla/5.0 (Windows; U; Windows NT 5.2) AppleWebKit/525.13 (KHTML, like Gecko) Version/3.1 Safari/525.13
    Mozilla/5.0 (iPhone; U; CPU like Mac OS X) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/4A93 Safari/419.3
    其版本号是Version之后的数字。

Opera

  • Opera提供了专门的浏览器标志,就是window.opera属性。Opera典型的userAgent如下:
    Opera/9.27 (Windows NT 5.2; U; zh-cn)
    Opera/8.0 (Macintosh; PPC Mac OS X; U; en)
    Mozilla/5.0 (Macintosh; PPC Mac OS X; U; en) Opera 8.0
    其中,版本号是靠近Opera的数字。

isIE () {
  // window.ActiveXObject 只支持IE
  if (!!window.ActiveXObject || ("ActiveXObject" in window)) {
    this.displayInternet = true
  } else {
    this.displayInternet = false
  }
},

判断所有浏览器类型

function myBrowser() {
    var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
    var isOpera = userAgent.indexOf("Opera") > -1; //判断是否Opera浏览器
    var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera; //判断是否IE浏览器
    var isEdge = userAgent.indexOf("Edge") > -1; //判断是否IE的Edge浏览器
    var isFirefox = userAgent.indexOf("Firefox") > -1; //判断是否Firefox浏览器
    var isSafari = userAgent.indexOf("Safari") > -1 && userAgent.indexOf("Chrome") == -1; //判断是否Safari浏览器
    var isChrome = userAgent.indexOf("Chrome") > -1 && userAgent.indexOf("Safari") > -1; //判断Chrome浏览器
    if (isIE) {
        var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
        reIE.test(userAgent);
        var fIEVersion = parseFloat(RegExp["$1"]);
        if (fIEVersion == 7) {
            return "IE7";
        } else if (fIEVersion == 8) {
            return "IE8";
        } else if (fIEVersion == 9) {
            return "IE9";
        } else if (fIEVersion == 10) {
            return "IE10";
        } else if (fIEVersion == 11) {
            return "IE11";
        } else {
            return "0";
        }//IE版本过低
        return "IE";
    }
    if (isOpera) { return "Opera"; }
    if (isEdge) { return "Edge"; }
    if (isFirefox) { return "Firefox"; }
    if (isSafari) { return "Safari"; }
    if (isChrome) { return "Chrome"; }
}

参考资料:
https://blog.csdn.net/u011020012/article/details/51209443

相关文章

网友评论

      本文标题:js判断浏览器类型

      本文链接:https://www.haomeiwen.com/subject/xxhzbktx.html