js識別手機訪問自動跳轉到相應頁面

來源:互聯網
上載者:User

標籤:智能   地址   webkit   cat   test   頭部   inpu   blog   app   

/** 智能機瀏覽器版本資訊:**/  var browser={    versions:function(){           var u = navigator.userAgent, app = navigator.appVersion;           return {//移動終端瀏覽器版本資訊                trident: u.indexOf("Trident") > -1, //IE核心                presto: u.indexOf("Presto") > -1, //opera核心                webKit: u.indexOf("AppleWebKit") > -1, //蘋果、Google核心                gecko: u.indexOf("Gecko") > -1 && u.indexOf("KHTML") == -1, //Firefox核心                mobile: !!u.match(/AppleWebKit.*Mobile.*/)||!!u.match(/AppleWebKit/), //是否為移動終端                ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios終端                android: u.indexOf("Android") > -1 || u.indexOf("Linux") > -1, //android終端或者uc瀏覽器                iPhone: u.indexOf("iPhone") > -1 || u.indexOf("Mac") > -1, //是否為iPhone或者QQHD瀏覽器                iPad: u.indexOf("iPad") > -1, //是否iPad                webApp: u.indexOf("Safari") == -1 //是否web應該程式,沒有頭部與底部            };         }(),         language:(navigator.browserLanguage || navigator.language).toLowerCase()}/** 調用方法:mobT()//放到那些需要跳轉的頁面底部**/function mobT(){    if(browser.versions.android || browser.versions.iPhone || browser.versions.ios){        var tourl="";        var url=document.location.href;        var sarr=url.split("/");//將url地址按"/"分段主要是用來擷取需要跳轉的頁面然後拼接        if(sarr[3]!=null){            if(sarr[3].indexOf("work")>-1 || sarr[3].indexOf("about")>-1 || sarr[3].indexOf("friend")>-1 || sarr[3].indexOf("photo")>-1);//判斷這些頁面是不需要跳轉的,因為我添加的是模板,所以要判斷。            else if(sarr[3].indexOf("index")==0)                tourl="m/";            else                       tourl="m/"+sarr[3];//拼接 以http://www.86y.org/art_detail.aspx?id=583為例,需要擷取只是art_detail.aspx?id=583部分,所以手機頁面是 m/art_detail.aspx?id=583。依此類推!        }        else            tourl="m/index.aspx" //首頁拼接        window.location.href=tourl;    }}

------------------------------------第二種------------------------------------------------------

電腦端地址:例:(86y.org)
手機端地址:例:目錄(86y.org/m)或次層網域(m.86y.org)

function mobT(){ var sUserAgent = navigator.userAgent.toLowerCase(); var tourl=""; var url=document.location.href; var pcurl="86y.org"; var moblieurl="86y.org/m"; if ((sUserAgent.match(/(ipod|iphone os|midp|ucweb|android|windows ce|windows mobile)/i))) { //pc端轉手機端 if(url.indexOf(moblieurl)==-1) { tourl=url.replace(pcurl,moblieurl); window.location.href=tourl; } } else{ //手機端轉pc端 if(url.indexOf("http://styhsoft.eicp.net:4658/test/m/")!=-1) { tourl=url.replace(moblieurl,pcurl); window.location.href=tourl; } } } mobT();

 

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.