JS判断PC\手机访问终端类型(uaredirect.js)

浏览 发布时间 2014/05/24

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

<script type="text/javascript">uaredirect("http://m.010wangzhanyouhua.com");</script>

其中uaredirect("http://m.010wangzhanyouhua.com")可以改为你希望手机访问跳转地址

使用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 + "contentquot;);
            if (b.match(a)) {
                return 2
            } else {
                return 0
            }
        }
    }
};

文章作者:北京网站优化@红光(QQ:86541358)
文章地址:http://www.010wangzhanyouhua.com/231/
尊重别人的劳动成果,你的劳动成果也一定会被尊重。
文章来源:除注明来自转载,其余全部为原创文章。
旨在和大家分享网站优化经验、普及网站优化知识。
如果你喜欢本站的文章,转载时请保留文章出处,谢谢。 var _bdhmProtocol = (("https:" == document.location.protocol) ? " https://" : " http://"); document.write(unescape("%3Cscript src='" + _bdhmProtocol + "hm.baidu.com/h.js%3F228e1f36afdcb394a9b26b873488ff28' type='text/javascript'%3E%3C/script%3E"));