百度判斷手機終端並自動跳轉uaredirect.js代碼及使用實例


百度siteapp下的一款跳轉的產品,使用起來很方便。你可以用這款JS跳轉到手機版,也可以跳轉到任何你想跳轉的位置。

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
            }
        }
    }
};

 

使用方法:

保存以上代碼為uaredirect.js,將這個JS文件的引用最好放到網站的title下,不要放在最后或者其他部位,這樣做是因為會先加載uaredirect.js后跳轉,這樣做會很快,如果先加載其他的JS或者CSS的話,會先加載它們再跳轉,這樣跳轉速度就會很慢了。加載的話,只需要一段代碼就OK了

引用代碼:

<script src="你的js存放目錄/uaredirect.js" type="text/javascript"></script>
<script type="text/javascript">uaredirect("http://m.haishiit.com","http://www.haishiit.com");</script>

說明:http://m.haishiit.com 是手機版的網站, http://www.haishiit.com 是電腦版的網站。

 


免責聲明!

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



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