1. 第一种:通过判断浏览器的userAgent,用正则来判断是否是ios和Android客户端
User Agent中文名为用户代理,是Http协议中的一部分,属于头域的组成部分,User Agent也简称UA。它是一个特殊字符串头,是一种向访问网站提供你所使用的浏览器类型及版本、操作系统及版本、浏览器内核、等信息的标识。通过这个标 识,用户所访问的网站可以显示不同的排版从而为用户提供更好的体验或者进行信息统计;例如用手机访问谷歌和电脑访问是不一样的,这些是谷歌根据访问者的 UA来判断的。UA可以进行伪装。
浏览器的UA字串的标准格式:浏览器标识 (操作系统标识; 加密等级标识; 浏览器语言) 渲染引擎标识版本信息。但各个浏览器有所不同。
代码如下:
| var u = navigator.userAgent; |
| var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; |
| var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); |
| alert('是否是Android:'+isAndroid); |
| alert('是否是iOS:'+isiOS); |
2. 第二种:检查是否是移动端(Mobile)、ipad、iphone、微信、QQ等。
2.1 代码如下:
| 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('Adr') > -1, |
| iPhone: u.indexOf('iPhone') > -1 , |
| iPad: u.indexOf('iPad') > -1, |
| webApp: u.indexOf('Safari') == -1, |
| weixin: u.indexOf('MicroMessenger') > -1, |
| qq: u.match(/\sQQ/i) == " qq" |
| }; |
| }(), |
| language:(navigator.browserLanguage || navigator.language).toLowerCase() |
| } |
2.2 使用方法
| |
| if(browser.versions.trident){ alert("is IE"); } |
| |
| if(browser.versions.webKit){ alert("is webKit"); } |
| |
| if(browser.versions.mobile||browser.versions.android||browser.versions.ios){ alert("移动端"); } |
2.3 检测浏览器语言
| currentLang = navigator.language; |
| if(!currentLang){ |
| currentLang = navigator.browserLanguage; |
| } |
| alert(currentLang); |
3. 判断iPhone|iPad|iPod|iOS|Android客户端
代码如下:
| if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) { |
| |
| |
| window.location.href ="iPhone.html"; |
| } else if (/(Android)/i.test(navigator.userAgent)) { |
| |
| |
| window.location.href ="Android.html"; |
| } else { |
| |
| window.location.href ="pc.html"; |
| }; |
4. 判断pc还是移动端
代码如下:
| |
| var userAgentInfo = navigator.userAgent.toLowerCase(); |
| var Agents = ["android", "iphone","symbianos", "windows phone","ipad", "ipod"]; |
| var ly=document.referrer; |
| for (var v = 0; v < Agents.length; v++) { if (userAgentInfo.indexOf(Agents[v]) >= 0&&(ly==""||ly==null)) { |
| this.location.href='http://m.***.com'; |
| } |
| } |
5. 常用跳转代码
看代码
| |
| (function browserRedirect(){ |
| var sUserAgent = navigator.userAgent.toLowerCase(); |
| var bIsIpad = sUserAgent.match(/ipad/i) == 'ipad'; |
| var bIsIphone = sUserAgent.match(/iphone os/i) == 'iphone os'; |
| var bIsMidp = sUserAgent.match(/midp/i) == 'midp'; |
| var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == 'rv:1.2.3.4'; |
| var bIsUc = sUserAgent.match(/ucweb/i) == 'web'; |
| var bIsCE = sUserAgent.match(/windows ce/i) == 'windows ce'; |
| var bIsWM = sUserAgent.match(/windows mobile/i) == 'windows mobile'; |
| var bIsAndroid = sUserAgent.match(/android/i) == 'android'; |
| var pathname = location.pathname |
| if(bIsIpad || bIsIphone || bIsMidp || bIsUc7 || bIsUc || bIsCE || bIsWM || bIsAndroid ){ |
| window.location.href = 'http://m.geekjc.com'+pathname; |
| } |
| })(); |
Comments | NOTHING