ie
var theUA = window.navigator.userAgent.toLowerCase();console.log(theUA);//ieif ((theUA.match(/msie\s\d+/) && theUA.match(/msie\s\d+/)[0])|| (theUA.match(/trident\s?\d+/) && theUA.match(/trident\s?\d+/)[0])) {var ieVersion = theUA.match(/msie\s\d+/)[0].match(/\d+/)[0]|| theUA.match(/trident\s?\d+/)[0];console.log(ieVersion);}
Android、iphone、ipad
var n = navigator.userAgent.toLowerCase();if (n.indexOf('Android') > -1 || n.indexOf('Linux') > -1) {console.log("安卓手机");} else if (n.indexOf('iPhone') > -1) {console.log("苹果手机");} else if (n.indexOf('Windows Phone') > -1) {console.log("winphone手机");} else if (n.indexOf('Google Phone') > -1) {console.log("谷歌手机");}
浏览器
var ua=navigator.userAgent.toLowerCase();if(/msie/i.test(ua) && !/opera/.test(ua)){alert("IE");return ;}else if(/firefox/i.test(ua)){alert("Firefox");return ;}else if(/chrome/i.test(ua) && /webkit/i.test(ua) && /mozilla/i.test(ua)){alert("Chrome");return ;}else if(/opera/i.test(ua)){alert("Opera");return ;}else if(/iPad/i){alert("ipad");return ;}if(/webkit/i.test(ua) &&!(/chrome/i.test(ua) && /webkit/i.test(ua) && /mozilla/i.test(ua))){alert("Safari");return ;}else{alert("unKnow");}
应用
有些软件是内置的浏览器,比如新浪微博、腾讯QQ(非QQ浏览器)和微信
(微信在6.0.2版本的时候做了改动,微信的分享功能在新版本跟以前不一样了)为了兼容版本,要做以下操作:
注:新浪微博为1,QQ客户端为2,微信低于6.0.2版本为3,高于6.0.2版本为4,其他为0。
var ua = navigator.userAgent.toLowerCase();if(ua.match(/weibo/i) == "weibo"){console.log(1);}else if(ua.indexOf('qq/')!= -1){console.log(2);}else if(ua.match(/MicroMessenger/i)=="micromessenger"){var v_weixin = ua.split('micromessenger')[1];v_weixin = v_weixin.substring(1,6);v_weixin = v_weixin.split(' ')[0];if(v_weixin.split('.').length == 2){v_weixin = v_weixin + '.0';}if(v_weixin < '6.0.2'){console.log(3);}else{console.log(4);}}else{console.log(0);}
