话不多说,直接看代码
1、区分Android、iphone、ipad:
1var ua = navigator.userAgent.toLowerCase(); 2if (/android|adr/gi.test(ua)) { 3 // 安卓 4 5}else if(/\(i[^;]+;( U;)? CPU.+Mac OS X/gi.test(ua)){ 6 //苹果 7 8}else if(/iPad/gi.test(ua)){ 9 //ipad 10 11}
2、区分设备: 新浪微博为1,QQ客户端为2,微信低于6.0.2版本为3,高于6.0.2版本为4,其他为0。
1var ua = navigator.userAgent.toLowerCase(); 2if(ua.match(/weibo/i) == "weibo"){ 3 console.log(1); 4}else if(ua.indexOf('qq/')!= -1){ 5 console.log(2); 6}else if(ua.match(/MicroMessenger/i)=="micromessenger"){ 7var v_weixin = ua.split('micromessenger')[1]; 8 v_weixin = v_weixin.substring(1,6); 9 v_weixin = v_weixin.split(' ')[0]; 10if(v_weixin.split('.').length == 2){ 11 v_weixin = v_weixin + '.0'; 12} 13if(v_weixin < '6.0.2'){ 14 console.log(3); 15}else{ 16 console.log(4); 17} 18}else{ 19 console.log(0); 20}
3、区分各个浏览器
1var ua=navigator.userAgent.toLowerCase(); 2 if(/msie/i.test(ua) && !/opera/.test(ua)){ 3 alert("IE"); 4 return ; 5 }else if(/firefox/i.test(ua)){ 6 alert("Firefox"); 7 return ; 8 }else if(/chrome/i.test(ua) && /webkit/i.test(ua) && /mozilla/i.test(ua)){ 9 alert("Chrome"); 10 return ; 11 }else if(/opera/i.test(ua)){ 12 alert("Opera"); 13 return ; 14 }else if(/iPad/i){ 15 alert("ipad"); 16 return ; 17 } 18else if(/webkit/i.test(ua) &&!(/chrome/i.test(ua) && /webkit/i.test(ua) && /mozilla/i.test(ua))){ 19 alert("Safari"); 20 return ; 21 }else{ 22 alert("unKnow"); 23 }
完