判断是手机端还是PC短访问

第一种:判断是手机访问还是PC访问

<script>
  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) ){
      //跳转到PC端地址
      window.location.href='http://www.jd.com';
    }else{
      //跳转到移动端地址
      window.location.href='http://m.jd.com';
    }
  }

  browserRedirect();
</script>

 

第二种:判断是手机访问还是PC访问


var system ={};
var p = navigator.platform;
system.win = p.indexOf("Win") == 0;
system.mac = p.indexOf("Mac") == 0;
system.x11 = (p == "X11") || (p.indexOf("Linux") == 0);
if(system.win||system.mac||system.xll){//如果是电脑跳转到百度
  window.location.href="http://www.baidu.com/";
}else{ //如果是手机,跳转到谷歌
  window.location.href="http://www.google.cn/";
}

 

第三种:判断是手机访问还是PC访问


<script>

if(navigator.platform.indexOf('Win32')!=-1){
  //pc
  //window.location.href="电脑网址";
}else{
  //手机
  window.location.href="手机网址";
}
</script>


第四种:百度判断是手机还是PC访 问


<script src="http://siteapp.baidu.com/static/webappservice/uaredirect.js"type="text/javascript"></script>
<script type="text/javascript">uaredirect("http://m.XXX.com")//手机网址;</script>

将代码网址部分改成你网站的手机网址就Ok,当然可以是任意网址!

 

第五种:判断是手机还是PC跳转到指定的地址*

下面这段代码是判断浏览器是手机浏览器,然后自动跳转到mobile.com,怎么修改,才能
把不是用手机浏览器的人,在上mobile.com的时候,自动跳转到PC.COM

<script type="text/javascript">

if(/AppleWebKit.*Mobile/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA| SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia| SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))){
  if(window.location.href.indexOf("?mobile")<0){
    try{
      if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)){
        //手机
        window.location.href="http://www.mobile.com";
      }else if(/iPad/i.test(navigator.userAgent)){
        //ipad
        window.location.href="http://ipad.com"
      }else{
        //电脑
        window.location.href="http://pc.com"
      }
    }catch(e){}
  }
}
</script>

 

转载于:https://www.cnblogs.com/wakbook/p/6429804.html

评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符 “速评一下”
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页