代码一:
function urlredirect() {var sUserAgent = navigator.userAgent.toLowerCase();if ((sUserAgent.match(/(ipod|iphone os|midp|ucweb|android|windows ce|windows mobile)/i))) {var thisUrl = window.location.href;window.location.href = thisUrl.replace("www", "m");}}urlredirect();
举例:
手机访问自动跳转
自动识别手机访问或PC访问,手机访问,自动跳转,推荐使用。
代码二:
function isMobile() {if ((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i))) {return true;} else {return false;}}if (isMobile() !== false) {window.location.href = "移动端网址";}
此法需配置自己的移动端网址,否则不能丝滑跳转。
总结:
上面两种方法需放在<script type="text/javascript"></script>中使用,代码简洁实用。