[前端]根据url判断手机跳转,所有网站通用

deegao  于 2019-3-29 21:21:49 回帖奖励 |阅读模式
0 3554
仅适合手机站url后缀参数和pc一致的用户,
如:www.aa.com/a/1.html  对应的是 m.aa.com/a/11.html

js跳转
代码如下

  1. function GetUrlRelativePath(){
  2.     var url = document.location.toString();
  3.     var arrUrl = url.split("//");


  4.     var start = arrUrl[1].indexOf("/");
  5.     var relUrl = arrUrl[1].substring(start);//stop省略,截取从start开始到结尾的所有字符


  6.     if(relUrl.indexOf("?") != -1){
  7.       relUrl = relUrl.split("?")[0];
  8.     }
  9.     return relUrl;
  10.   }


  11. function mobile_device_detect(url)
  12. {
  13.         var thisOS=navigator.platform;
  14.         var os=new Array("iPhone","iPod","iPad","android","Nokia","SymbianOS","Symbian","Windows Phone","Phone","Linux armv71","MAUI","UNTRUSTED/1.0","Windows CE","BlackBerry","IEMobile");
  15. for(var i=0;i<os.length;i++)
  16.         {
  17. if(thisOS.match(os[i]))
  18.         {   
  19.   window.location=url;
  20. }
  21.   
  22. }
  23. //因为相当部分的手机系统不知道信息,这里是做临时性特殊辨认
  24. if(navigator.platform.indexOf('iPad') != -1)
  25.         {
  26.   window.location=url;
  27. }
  28. //做这一部分是因为Android手机的内核也是Linux
  29. //但是navigator.platform显示信息不尽相同情况繁多,因此从浏览器下手,即用navigator.appVersion信息做判断
  30.   var check = navigator.appVersion;
  31.   if( check.match(/linux/i) )
  32.           {
  33.    //X11是UC浏览器的平台 ,如果有其他特殊浏览器也可以附加上条件
  34.    if(check.match(/mobile/i) || check.match(/X11/i))
  35.                  {
  36.    window.location=url;
  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. mobile_device_detect("http://m.abc.com"+GetUrlRelativePath());
复制代码


| 0 人收藏
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
回复

使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    • 售后服务
    • 关注我们
    • 社区新手

    QQ|Archiver|手机版|小黑屋|德高网络.  

    Powered by Discuz! X3.2  © 2001-2018 Comsenz Inc.