js判断页面是否为手机端访问
var ua = navigator.userAgent; var ipad = ua.match(/(iPad).*OSs([d_]+)/), isIphone = !ipad && ua.match(/(iPhonesOS)s([d_]+)/), isAndroid = ua.match(/(Android)s+([d.]+)/), isMobile = isIphone || isAndroid; if(isMobile) { location.href = 'http://m.baidu.com'; }else{ location.href = 'http://www.baidu.com'; } //或者单独判断iphone或android if(isIphone){ console.log("iphone访问"); //code }else if(isAndroid){ console.log("Android访问"); //code }else{ console.log("非iphone或Android访问"); //code }
js判断页面是手机端还是PC端打开
//方法2
<script type="text/javascript"> function browserRedirect() { var sUserAgent = navigator.userAgent.toLowerCase(); var bIsIpad = sUserAgent.match(/ipad/i) == "ipad"; var bIsIphoneOs = 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) == "ucweb"; var bIsAndroid = sUserAgent.match(/android/i) == "android"; var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce"; var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile"; if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) { //跳转移动端页面 window.location.href="http://f.jcngame.com/fanfan20171208/mobile/index.html"; } else { //跳转pc端页面 window.location.href="http://f.jcngame.com/fanfan20171208//fanmai/index.html"; } } browserRedirect(); </script>
//方法3
<script> var browser={ versions:function(){ var u = navigator.userAgent, app = navigator.appVersion; return {//移动终端浏览器版本信息 trident: u.indexOf('Trident') > -1, //IE内核 presto: u.indexOf('Presto') > -1, //opera内核 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/), //ios终端 android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器 iPhone: u.indexOf('iPhone') > -1 , //是否为iPhone或者QQHD浏览器 iPad: u.indexOf('iPad') > -1, //是否iPad webApp: u.indexOf('Safari') == -1, //是否web应该程序,没有头部与底部 weixin: u.indexOf('MicroMessenger') > -1, //是否微信 qq: u.match(/sQQ/i) == " qq" //是否QQ }; }(), language:(navigator.browserLanguage || navigator.language).toLowerCase() } if(browser.versions.mobile || browser.versions.ios || browser.versions.android || browser.versions.iPhone || browser.versions.iPad){ //web端打开 window.location = "http://m.zhaizhainv.com"; }else if(browser.versions.weixin){ //微信打开 }else{ //PC端打开 } </script>
//另:智能机浏览器版本信息:
<script type="text/javascript"> var browser={ versions:function(){ var u = navigator.userAgent, app = navigator.appVersion; return {//移动终端浏览器版本信息 trident: u.indexOf('Trident') > -1, //IE内核 presto: u.indexOf('Presto') > -1, //opera内核 webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核 gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核 mobile: !!u.match(/AppleWebKit.*Mobile.*/)||u.indexOf('iPad') > -1, //是否为移动终端 ios: !!u.match(/(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端 android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器 iPhone: u.indexOf('iPhone') > -1, //是否为iPhone或者QQHD浏览器 iPad: u.indexOf('iPad') > -1, //是否iPad webApp: u.indexOf('Safari') == -1 //是否web应该程序,没有头部与底部 }; }(), language:(navigator.browserLanguage || navigator.language).toLowerCase() } document.writeln("语言版本: "+browser.language+"<br/>"); document.writeln("是否为移动终端: "+browser.versions.mobile+"<br/>"); document.writeln("ios终端: "+browser.versions.ios+"<br/>"); document.writeln("android终端: "+browser.versions.android+"<br/>"); document.writeln("是否为iPhone: "+browser.versions.iPhone+"<br/>"); document.writeln("是否iPad: "+browser.versions.iPad+"<br/>"); document.writeln(navigator.userAgent); </script>
//浏览器是否为移动端
<script> if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) { window.location.href = "https://www.baidu.com/"; //手机端 } else { window.location.href = "http://news.baidu.com/"; } </script>
是否为ipad
<script> //判断是否使用的是ipad function isIpad(){ var ua = navigator.userAgent.toLowerCase(); if(/ipad/i.test(ua)) { return true; } else{ return false; } </script>
//延展:
<!--js判断浏览器是否为IE8以下--> <script> var DEFAULT_VERSION = 8.0; var ua = navigator.userAgent.toLowerCase(); var isIE = ua.indexOf("msie")>-1; var safariVersion; if(isIE){ safariVersion = ua.match(/msie ([d.]+)/)[1]; } if(safariVersion < DEFAULT_VERSION ){ // 进行你所要的操作 alert("系统检测到您正在使用ie8以下内核的浏览器,为了更好的体验,请及时更新浏览器版本或采用浏览器极速模式浏览网页!") }; </script>
//是否为IE
<script> function isIE() { //ie? if (!!window.ActiveXObject || "ActiveXObject" in window){ alert("我是货真价实的IE浏览器!"); return true; }else{ alert("我不是IE!"); return false; } } isIE(); </script>
参考链接:https://blog.csdn.net/linghunyoushou/article/details/92760638