美文网首页
js判断浏览器

js判断浏览器

作者: 小小了墨 | 来源:发表于2019-08-13 11:51 被阅读0次

    判断浏览器

    function isBroswer () {//检测浏览器内核--返回的是两个key,name:浏览器内核的名称---version:浏览器的版本号
           var _broswer = {};
           var sUserAgent = navigator.userAgent;
           var isOpera = sUserAgent.indexOf("Opera") > -1;
           if (isOpera) {
               //首先检测Opera是否进行了伪装
               if (navigator.appName == 'Opera') {
                   //如果没有进行伪装,则直接后去版本号
                   _broswer.version = parseFloat(navigator.appVersion);
               } else {
                   var reOperaVersion = new RegExp("Opera (\\d+.\\d+)");
                   //使用正则表达式的test方法测试并将版本号保存在RegExp.$1中
                   reOperaVersion.test(sUserAgent);
                   _broswer.version = parseFloat(RegExp['$1']);
               }
               _broswer.opera = true;
               _broswer.name = 'opera';
           }
           var isChrome = sUserAgent.indexOf("Chrome") > -1;
           if (isChrome) {
               var reChorme = new RegExp("Chrome/(\\d+\\.\\d+(?:\\.\\d+\\.\\d+))?");
               reChorme.test(sUserAgent);
               _broswer.version = parseFloat(RegExp['$1']);
               _broswer.chrome = true;
               _broswer.name = 'chrome';
           }
           //排除Chrome信息,因为在Chrome的user-agent字符串中会出现Konqueror/Safari的关键字
           var isKHTML = (sUserAgent.indexOf("KHTML") > -1
               || sUserAgent.indexOf("Konqueror") > -1 || sUserAgent
                   .indexOf("AppleWebKit") > -1)
               && !isChrome;
           if (isKHTML) {//判断是否基于KHTML,如果时的话在继续判断属于何种KHTML浏览器
               var isSafari = sUserAgent.indexOf("AppleWebKit") > -1;
               var isKonq = sUserAgent.indexOf("Konqueror") > -1;
               if (isSafari) {
                   var reAppleWebKit = new RegExp("Version/(\\d+(?:\\.\\d*)?)");
                   reAppleWebKit.test(sUserAgent);
                   var fAppleWebKitVersion = parseFloat(RegExp["$1"]);
                   _broswer.version = parseFloat(RegExp['$1']);
                   _broswer.safari = true;
                   _broswer.name = 'safari';
               } else if (isKonq) {
                   var reKong = new RegExp(
                       "Konqueror/(\\d+(?:\\.\\d+(?\\.\\d)?)?)");
                   reKong.test(sUserAgent);
                   _broswer.version = parseFloat(RegExp['$1']);
                   _broswer.konqueror = true;
                   _broswer.name = 'konqueror';
               }
           }
           // !isOpera 避免是由Opera伪装成的IE
           var isIE = sUserAgent.indexOf("compatible") > -1
               && sUserAgent.indexOf("MSIE") > -1 && !isOpera;
           if (isIE) {
               var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
               reIE.test(sUserAgent);
               _broswer.version = parseFloat(RegExp['$1']);
               _broswer.msie = true;
               _broswer.name = 'msie';
           }
           // 排除Chrome 及 Konqueror/Safari 的伪装
           var isMoz = sUserAgent.indexOf("Gecko") > -1 && !isChrome && !isKHTML;
           if (isMoz) {
               var reMoz = new RegExp("rv:(\\d+\\.\\d+(?:\\.\\d+)?)");
               reMoz.test(sUserAgent);
               _broswer.version = parseFloat(RegExp['$1']);
               _broswer.mozilla = true;
               _broswer.name = 'mozilla';
           }
           return _broswer;
        }
        
        // 判断ie的版本
        function IEVersion() {
            var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
            var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器
            var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器
            var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1;
            if(isIE) {
                var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
                reIE.test(userAgent);
                var fIEVersion = parseFloat(RegExp["$1"]);
                if(fIEVersion == 7) {
                    return 7;
                } else if(fIEVersion == 8) {
                    return 8;
                } else if(fIEVersion == 9) {
                    return 9;
                } else if(fIEVersion == 10) {
                    return 10;
                } else {
                    return 6;//IE版本<=7
                }
            } else if(isIE11) {
                return 11; //IE11
            }else{
                return 99;//不是ie浏览器
            }
        }
        var ieVersion = IEVersion();
        var notChromeBrowser = isBroswer();
        
        // 内核
        var browser={
      versions:function(){
        var u = navigator.userAgent, app = navigator.appVersion;
        return {
          trident: u.indexOf('Trident') > -1, //IE内核
          presto: u.indexOf('Presto') > -1, //opera内核
          webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
          gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1,//火狐内核
          mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
          ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
          android: u.indexOf('Android') > -1 || u.indexOf('Adr') > -1, //android终端
          iPhone: u.indexOf('iPhone') > -1 , //是否为iPhone或者QQHD浏览器
          iPad: u.indexOf('iPad') > -1, //是否iPad
          webApp: u.indexOf('Safari') == -1, //是否web应该程序,没有头部与底部
          weixin: u.indexOf('MicroMessenger') > -1, //是否微信 (2015-01-22新增)
          qq: u.match(/\sQQ/i) == " qq" //是否QQ
        };
      }(),
      language:(navigator.browserLanguage || navigator.language).toLowerCase()
    }
    
    

    相关文章

      网友评论

          本文标题:js判断浏览器

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