PC与手机访问自动检测跳转到相应页面
我们在做网页设计的时候经常需要用到PC端和手机移动端,用户使用PC访问PC端版面,使用手机则访问手机端版本,下面几行代码就解决了我们如何判断用户访问设备而自动跳转到相应界面。
1、移动端检测是否是PC端浏览器
<script data-fixed="true"> !function(){ if(/iphone|ios|android|ipod/i.test(navigator.userAgent.toLowerCase()) == false){ location.href = 'PC地址'; } }(); </script>
2、PC端检测是否是移动设备
<script data-fixed="true"> !function(){ function params(u, p){ var m = new RegExp("(?:&|/?)"+p+"=([^&$]+)").exec(u); return m ? m[1] : ''; } if(/iphone|ios|android|ipod/i.test(navigator.userAgent.toLowerCase()) == true && params(location.search, "from") != "mobile"){ location.href = '移动端地址'; } }(); </script
本文由白成建博客发布,转载联系作者并注明出处:https://www.baichengjian.com/jingyanfenxiang/320.html