首页 > 设计 > 实用代码 > 正文

手机访问pc网站自动跳转手机端网站代码

2020-09-01 23:00:01
字体:
来源:转载
供稿:网友
4G降临,移动网站已经一发不可收拾,pc端和移动端官网并存。如何让别人访问你的pc端的官网直接跳转到移动端的网站呢?各位看官,小二上代码!来了!
  1.  <SCRIPT LANGUAGE="JavaScript">
  2.  function mobile_device_detect(url)
  3.  {
  4.  
  5.         var thisOS=navigator.platform;
  6.  
  7.         var os=new Array("iPhone","iPod","iPad","android","Nokia","SymbianOS","Symbian","Windows Phone","Phone","Linux armv71","MAUI","UNTRUSTED/1.0","Windows CE","BlackBerry","IEMobile");
  8.  
  9.  for(var i=0;i<os.length;i++)
  10.         {
  11.  
  12.  if(thisOS.match(os[i]))
  13.         {   
  14.   window.location=url;
  15.  }
  16.   
  17.  }
  18.  
  19.  
  20.  //因为相当部分的手机系统不知道信息,这里是做临时性特殊辨认
  21.  if(navigator.platform.indexOf('iPad') != -1)
  22.         {
  23.   window.location=url;
  24.  }
  25.  
  26.  //做这一部分是因为Android手机的内核也是Linux
  27.  //但是navigator.platform显示信息不尽相同情况繁多,因此从浏览器下手,即用navigator.appVersion信息做判断
  28.   var check = navigator.appVersion;
  29.  
  30.   if( check.match(/linux/i) )
  31.           {
  32.    //X11是UC浏览器的平台 ,如果有其他特殊浏览器也可以附加上条件
  33.    if(check.match(/mobile/i) || check.match(/X11/i))
  34.                  {
  35.    window.location=url;
  36.    }  
  37.  }
  38.  
  39.  //类in_array函数
  40.  Array.prototype.in_array = function(e)
  41.  {
  42.   for(i=0;i<this.length;i++)
  43.   {
  44.    if(this[i] == e)
  45.    return true;
  46.   }
  47.   return false;
  48.  }
  49.  } 
  50.  
  51. mobile_device_detect("http://***.***.com");
  52.  
  53.  </SCRIPT>
复制代码
 
备注这里的
mobile_device_detect("http://***.***.com");//里面的地址填的就是您的移动端的网站地址呦。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表