更多



手机等移动设备访问PC端站点自动跳转到手机网站的跳转代码 不指定

郝聪 , 2018/04/10 11:33 , 建站优化 , 评论(3) , 阅读(5141) , Via 本站原创 | |

【跳转代码一】

XML/HTML代码
    ????
  1. <script?type="text/javascript">??
  2. ????
  3. try?{??
  4. ????
  5. var?urlhash?=?window.location.hash;??
  6. ????
  7. if?(!urlhash.match("fromapp"))??
  8. ????
  9. {??
  10. ????
  11. if?((navigator.userAgent.match(/(iPhone|iPod|Android|ios|iPad)/i)))??
  12. ????
  13. {??
  14. ????
  15. window.location="http://m.your-website.com/";?//这里的网址请改为你手机站的网址??
  16. ????
  17. }??
  18. ????
  19. }??
  20. ????
  21. }??
  22. ????
  23. catch(err)??
  24. ????
  25. {??
  26. ????
  27. }??
  28. ????
  29. script>??

【跳转代码二】(利用百度Site App代码判断手机访问)

XML/HTML代码
    ????
  1. <script?language="JavaScript">function?uaredirect(f){try{if(document.getElementById("bdmark")!=null){return}var?b=false;if(arguments[1]){var?e=window.location.host;var?a=window.location.href;if(isSubdomain(arguments[1],e)==1){ff=f+"/#m/"+a;b=true}else{if(isSubdomain(arguments[1],e)==2){ff=f+"/#m/"+a;b=true}else{f=a;b=false}}}else{b=true}if(b){var?c=window.location.hash;if(!c.match("fromapp")){if((navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i))){location.replace(f)}}}}catch(d){}}function?isSubdomain(c,d){this.getdomain=function(f){var?e=f.indexOf("://");if(e>0){var?h=f.substr(e+3)}else{var?h=f}var?g=/^www\./;if(g.test(h)){hh=h.substr(4)}return?h};if(c==d){return?1}else{var?c=this.getdomain(c);var?b=this.getdomain(d);if(c==b){return?1}else{cc=c.replace(".","\\.");var?a=new?RegExp("\\."+c+"$");if(b.match(a)){return?2}else{return?0}}}};uaredirect("m.your-website.com");script>
  2. ????
  3. <script?type="text/javascript">uaredirect("m.your-website.com");script>??
    ????

【跳转代码三】

XML/HTML代码
    ????
  1. <script language="JavaScript">??
  2. ????
  3. function?mobile_device_detect(url)??
  4. ????
  5. {??
  6. ????
  7. var?thisOS=navigator.platform;??
  8. ????
  9. var?os=new?Array("iPhone","iPod","iPad","android","Nokia","SymbianOS","Symbian","Windows?Phone","Phone","Linux?armv71","MAUI","UNTRUSTED/1.0","Windows?CE","BlackBerry","IEMobile");??
  10. ????
  11. for(var?i=0;i<os.length;i++)??
  12. ????
  13. {??
  14. ????
  15. if(thisOS.match(os[i]))??
  16. ????
  17. {??
  18. ????
  19. window.location=url;??
  20. ????
  21. }??
  22. ????
  23. ??
  24. ????
  25. }??
  26. ????
  27. //因为相当部分的手机系统不知道信息,这里是做临时性特殊辨认??
  28. ????
  29. if(navigator.platform.indexOf('iPad')?!=?-1)??
  30. ????
  31. {??
  32. ????
  33. window.location=url;??
  34. ????
  35. }??
  36. ????
  37. //做这一部分是因为Android手机的内核也是Linux??
  38. ????
  39. //但是navigator.platform显示信息不尽相同情况繁多,因此从浏览器下手,即用navigator.appVersion信息做判断??
  40. ????
  41. var?check?=?navigator.appVersion;??
  42. ????
  43. if(?check.match(/linux/i)?)??
  44. ????
  45. {??
  46. ????
  47. //X11是UC浏览器的平台?,如果有其他特殊浏览器也可以附加上条件??
  48. ????
  49. if(check.match(/mobile/i)?||?check.match(/X11/i))??
  50. ????
  51. {??
  52. ????
  53. window.location=url;??
  54. ????
  55. }??
  56. ????
  57. }??
  58. ????
  59. //类in_array函数??
  60. ????
  61. Array.prototype.in_array?=?function(e)??
  62. ????
  63. {??
  64. ????
  65. for(i=0;i<this.length;i++)??
  66. ????
  67. {??
  68. ????
  69. if(this[i]?==?e)??
  70. ????
  71. return?true;??
  72. ????
  73. }??
  74. ????
  75. return?false;??
  76. ????
  77. }??
  78. ????
  79. }??
  80. ????
  81. mobile_device_detect("http://m.your-website.com");??
  82. ????
  83. script>??
技能制造大师 Email
2019/04/26 20:09
好文章,好好学习,天天向上,支持一下
文娱帝国 Email
2019/03/30 19:32
写的很好,很喜欢
丘八 Email
2019/02/26 19:16
文章不错,非常喜欢
分页: 1/1 第一页 1 最后页
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]

?