因为项目要经常用要判断移动设备、pc端、android、iPhone、是否为微信、微博、qq空间等,这里记录一下各端环境的判断方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| var browser = {
versions: function () { var u = navigator.userAgent, app = navigator.appVersion; return { trident: u.indexOf('Trident') > -1, presto: u.indexOf('Presto') > -1, 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/), android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, iPhone: u.indexOf('iPhone') > -1, iPad: u.indexOf('iPad') > -1, webApp: u.indexOf('Safari') == -1 }; }(), language: (navigator.browserLanguage || navigator.language).toLowerCase() }
if (browser.versions.mobile) { var ua = navigator.userAgent.toLowerCase(); if (ua.match(/MicroMessenger/i) == "micromessenger") { } if (ua.match(/WeiBo/i) == "weibo") { } if (ua.match(/QQ/i) == "qq") { } if (browser.versions.ios) { } if(browser.versions.android){ } }else { }
|