
桌面端测试结果
【1】IE
[1.1]IE3
Mozilla/2.0 (compatible; MSIE3.02; windows 95)
[1.2]IE6
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
[1.3]IE7
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)
[1.4]IE8
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)
[1.5]IE9
Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
[1.6]IE10
Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)
[1.7]IE11
Mozilla/5.0 (MSIE 9.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET
CLR 3.0.30729; .NET4.0C; .NET4.0E; InfoPath.3; GWX:QUALIFIED; rv:11.0) like Gecko
【2】chrome
Mozilla/5.0 (Windows NT 6.1; WOW64)G AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36
【3】safari
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2
【4】firefox
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0
【5】opera
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36 OPR/32.0.1948.25
移动端测试结果
【1】ipad
Mozilla/5.0 (iPad; CPU OS 7_0 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53
【2】iphone
Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/600.1.3 (KHTML, like Gecko) Version/8.0 Mobile/12A4345d Safari/600.1.4
【3】android
Mozilla/5.0 (Linux; Android 4.2.2; GT-I9505 Build/JDQ39) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.59 Mobile Safari/537.36
识别浏览器内核
常见的内核有Trident、Gecko和Webkit
[注意]因为Trident和Webkit的用户代理字符串中可能会出现 like Gecko的字眼,所以最后再测Gecko
function whichEngine(){ var ua = navigator.userAgent; //Trident内核 if(/Trident/.test(ua)){return "Trident"; } //Webkit内核 if(/WebKit/.test(ua)){return "WebKit"; } //Gecko内核 if(/Gecko/.test(ua)){return "Gecko"; }}console.log(whichEngine());//IE11下显示"Trident"识别浏览器版本function isIE(){ var ua = navigator.userAgent; //检测Trident引擎,IE8+ if(/Trident/.test(ua)){//IE11+if(/rv:(d+)/.test(ua)){ return RegExp["$1"];} //IE8-IE10 if(/MSIE (d+)/.test(ua)){ return RegExp["$1"];} } //检测IE标识,IE7- if(/MSIE (d+)/.test(ua)){return RegExp["$1"]; } }console.log(isIE());//只有IE会返回版本号,其他浏览器都返回undefinedfunction isChrome(){ var ua = navigator.userAgent; //先排除opera,因为opera只是在chrome的userAgent后加入了自己的标识 if(!/OPR/.test(ua)){if(/Chrome/(S+)/.test(ua)){ return RegExp["$1"];}} }console.log(isChrome());//只有Chrome会返回版本号45.0.2454.93,其他浏览器都返回undefinedfunction isSafari(){ var ua = navigator.userAgent; //先排除opera if(!/OPR/.test(ua)){//检测出chrome和safari浏览器if(/Safari/.test(ua)){ //检测出safari if(/Version/(S+)/.test(ua)){return RegExp["$1"]; }} } }console.log(isSafari());//只有safari会返回版本号5.1.7,其他浏览器都返回undefinedfunction isFireFox(){ if(/Firefox/(S+)/.test(navigator.userAgent)){return RegExp["$1"]; } }console.log(isFireFox());//只有firefox会返回版本号40.0,其他浏览器都返回undefined【5】operafunction isOpera(){ if(/OPR/(S+)/.test(navigator.userAgent)){return RegExp["$1"]; } }console.log(isOpera());//只有opera会返回版本号32.0.1948.25,其他浏览器都返回undefined 识别操作系统function whichSyStem(){ var ua = navigator.userAgent; var pf = navigator.platform; if(/Mac/.test(pf)){return "Mac"; } if(/X11/.test(pf) || /Linux/.test(pf)){return "Linux"; } if(/Win/.test(pf)){if(/Windows NT (d+.d+)/.test(ua)){ switch(RegExp["$1"]){case "5.0": return "Windows 2000";case "5.1": return "Windows XP";case "6.0": return "Windows Vista";case "6.1": return "Windows 7";case "6.2": return "Windows 8";case "6.3": return "Windows 8.1";case "6.4":case "10": return "Windows 10";}} }}console.log(whichSyStem())//Windows 7识别移动端设备function whichMobile(){ var ua = navigator.userAgent; if(/iPhone OS (d+_d+)/.test(ua)){return "iPhone Mac" + RegExp.$1.replace("_","."); } if(/iPad.+OS (d+_d+)/.test(ua)){return "iPad Mac" + RegExp.$1.replace("_",".") } if(/Android (d+.d+)/.test(ua)){return "Android" + RegExp["$1"]; }}console.log(whichMobile())//Android 5.1