實現手機訪問,自動跳轉網站手機版


百度Site App的uaredirect.js實現手機訪問,自動跳轉網站手機版

以下為代碼,可放置在網站foot底部文件,或者haead頂部文件,建議將代碼放在網站頂部,這樣可以實現手機訪問立即跳轉!

<script src="http://siteapp.baidu.com/static/webappservice/uaredirect.js" type="text/javascript"></script><script type="text/javascript">uaredirect("http://m.jiuquan.cc");</script>

將代碼網址部分改成你網站的手機網址就Ok,當然可以是任意網址!

這應該就是傳說中的WAP自動跳轉吧!

轉自:鏈接

 

 

JS判斷用戶訪問終端類型(uaredirect.js)

不要在使用之前網上的那一大串判斷代碼了。

之前使用emlog的時候有人使用過這個方法,現在發現百度siteapp也提供這種方法

<script src="http://siteapp.baidu.com/static/webappservice/uaredirect.js" type="text/javascript"></script>
<script type="text/javascript">uaredirect("http://m.baidu.com");</script>

使用userAgent來判斷(iPhone|iPod|Android|ios|iPad)

function uaredirect(murl) {
    try {
        if (document.getElementById("bdmark") != null) {
            return;
        }
        var urlhash = window.location.hash;
        if (!urlhash.match("fromapp")) {
            if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios|iPad)/i))) {
                location.replace(murl);
            }
        }
    } catch(err) {}
}

uaredirect.js

function uaredirect(f) {
    try {
        if (document.getElementById("bdmark") != null) {
            return
        }
        var b = false;
        if (arguments[1]) {
            var e = window.location.host;
            var a = window.location.href;
            if (isSubdomain(arguments[1], e) == 1) {
                f = f + "/#m/" + a;
                b = true
            } else {
                if (isSubdomain(arguments[1], e) == 2) {
                    f = f + "/#m/" + a;
                    b = true
                } else {
                    f = a;
                    b = false
                }
            }
        } else {
            b = true
        }
        if (b) {
            var c = window.location.hash;
            if (!c.match("fromapp")) {
                if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i))) {
                    location.replace(f)
                }
            }
        }
    } catch(d) {}
}
function isSubdomain(c, d) {
    this.getdomain = function(f) {
        var e = f.indexOf("://");
        if (e > 0) {
            var h = f.substr(e + 3)
        } else {
            var h = f
        }
        var g = /^www./;
        if (g.test(h)) {
            h = h.substr(4)
        }
        return h
    };
    if (c == d) {
        return 1
    } else {
        var c = this.getdomain(c);
        var b = this.getdomain(d);
        if (c == b) {
            return 1
        } else {
            c = c.replace(".", "\.");
            var a = new RegExp("\." + c + "$");
            if (b.match(a)) {
                return 2
            } else {
                return 0
            }
        }
    }
};

壓縮代碼

function uaredirect(f){try{if(document.getElementById("bdmark")!=null){return}var b=false;if(arguments[1]){var e=window.location.host;var a=window.location.href;if(isSubdomain(arguments[1],e)==1){f=f+"/#m/"+a;b=true}else{if(isSubdomain(arguments[1],e)==2){f=f+"/#m/"+a;b=true}else{f=a;b=false}}}else{b=true}if(b){var c=window.location.hash;if(!c.match("fromapp")){if((navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i))){location.replace(f)}}}}catch(d){}}function isSubdomain(c,d){this.getdomain=function(f){var e=f.indexOf("://");if(e>0){var h=f.substr(e+3)}else{var h=f}var g=/^www./;if(g.test(h)){h=h.substr(4)}return h};if(c==d){return 1}else{var c=this.getdomain(c);var b=this.getdomain(d);if(c==b){return 1}else{c=c.replace(".","\.");var a=new RegExp("\."+c+"$");if(b.match(a)){return 2}else{return 0}}}};
轉自: 鏈接

 
 
 
 

通過js文件實現訪問網站自動跳轉到手機站

通過百度Site App的建站,可以通過在網站頭部head區間添加JS判斷,如果是手機訪問則自動跳轉到手機地址:

<script src="http://siteapp.baidu.com/static/webappservice/uaredirect.js" type="text/javascript"></script><script type="text/javascript">uaredirect("http://m.429006.com");</script>

仔細看了下這個js文件:uaredirect.js

function uaredirect(murl){
try {
if(document.getElementById("bdmark") != null){
return;
}
var urlhash = window.location.hash;
if (!urlhash.match("fromapp")){
if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios|iPad)/i))) {
location.replace(murl);
}
}
} catch(err){}
}

這個是一個移動設備判斷,使用userAgent來判斷的,iPhone|iPod|Android|ios|iPad,哥,你不是這些系統或設備的就靠邊。。。

這里給我們一個啟發,以后就可以用這個js去做判斷設備,然后做一些你想做的事情。

轉自:鏈接

 

下面是中國好買手自動跳轉在PC端項目起始頁面,檢測到是 從手機端訪問的,就自動跳轉到wap服務

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM