首页 > 代码库 > JS判断是否来自手机并跳转到手机版
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
JS判断是否来自手机并跳转到手机版
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。