使用javascript(JS)判断浏览器是否为移动版浏览器。
浏览器信息获取
判断浏览器访问终端。
1//判断访问终端 2var browser = { 3 versions: function () { 4 var u = navigator.userAgent, app = navigator.appVersion; 5 return { 6 trident: u.indexOf('Trident') > -1, //IE内核 7 presto: u.indexOf('Presto') > -1, //opera内核 8 webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核 9 gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核 10 mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端 11 ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端 12 android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器 13 iPhone: u.indexOf('iPhone') > -1, //是否为iPhone或者QQHD浏览器 14 iPad: u.indexOf('iPad') > -1, //是否iPad 15 webApp: u.indexOf('Safari') == -1, //是否web应该程序,没有头部与底部 16 weixin: u.indexOf('MicroMessenger') > -1, //是否微信 (2015-01-22新增) 17 qq: u.match(/\sQQ/i) == " qq" //是否QQ 18 }; 19 }(), 20 language: (navigator.browserLanguage || navigator.language).toLowerCase() 21};
判断是否为移动端
确认是否为移动版浏览器。
1// 判断是否为移动端 2if (browser.versions.mobile || browser.versions.android || browser.versions.ios) { 3 console.info("移动端"); 4 // window.location.href = "app/index.html"; 5} else { 6 console.info("非移动端"); 7 // window.location.href = "index.html"; 8}
【参考资料】