JS判斷是否來自手機並跳轉到手機版

來源:互聯網
上載者:User

標籤: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 & mcif(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 pageif (mUrl.indexOf(‘m.a.com‘) != -1) {window.location.href = mUrl;return;}        }             }         }     }  isMobilePhone();

本文出自 “風吹雪落” 部落格,請務必保留此出處http://cbsizhang.blog.51cto.com/7436956/1595805

JS判斷是否來自手機並跳轉到手機版

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.