JS判断是否来自手机并跳转到手机版
function isMobilePhone() { function getCookie(sName) { var sCookies = document.cookie ; if ("string" == typeof(sName)) { var sRE = "(?:; )?"+sName+"=([^;]*);?" ; var reRE = new RegExp(sRE) ; if (reRE.test(sCookies)) { return decodeURIComponent(RegExp["$1"]) ; } else { return null ; } } else { return sCookies ; } } var pcPageTip = ‘您将要跳转到电脑版网页,会使用较多手机上网流量,是否继续?‘; var cUrl = window.location.href; var referrer = document.referrer; var mIndex = ‘http://m.a.com/?v=1‘; var mFlag = window.location.hash, pcmFlag = getCookie(‘pcm‘); /* if(mFlag == ‘#m‘){ if (confirm(pcPageTip)) { return; } } */ var userAgent = navigator.userAgent; if(userAgent){ userAgent = userAgent.toUpperCase(); if(userAgent.indexOf("PAD")>-1){ return; } var mobilePhoneList = ["IOS","IPHONE","ANDROID","WINDOWS PHONE"]; for(var i=0,len=mobilePhoneList.length;i<len;i++){ if(userAgent.indexOf(mobilePhoneList[i])>-1){ function isPcPage(url) { var pcDomains = [‘sale.a.com‘, ‘pinpaijie.a.com‘]; for (var i = 0, len = pcDomains.length; i < len; i++) { if (url.indexOf(pcDomains[i]) != -1) return true; } return false; } var mUrl = cUrl.replace(/sale\.a\.com\/act/,‘m.a.com/sale/act‘); // no sale m & mc if(isPcPage(mUrl) || mFlag == ‘#m‘ || pcmFlag == ‘1‘) { if (confirm(pcPageTip)) { return; } else { if (referrer && referrer != ‘‘) { if (referrer === cUrl) { window.location.href = mIndex; return; } else { window.location.href = referrer; return; } } else { window.location.href = mIndex; return; } } } // M page if (mUrl.indexOf(‘m.a.com‘) != -1) { window.location.href = mUrl; return; } } } } } isMobilePhone();
本文出自 “风吹雪落” 博客,请务必保留此出处http://cbsizhang.blog.51cto.com/7436956/1595805
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。