用百度siteapp的uaredirect.js判断用户访问端而进行域名的自动跳转,并通过cookie记录手机访问电脑端的状态

如果你的系统有触屏版和电脑版,一般就意味着你有两个域名。
如果你有两个域名,那么当用户来访问的时候就需要做域名访问判断了。
需要实现当用户使用手机输入了电脑版的域名自动跳转到触屏版。

本文介绍的是利用百度siteapp的uaredirect.js文件。
首先在你的head中嵌入这段代码

<script src="${base}/resources/shop/${theme}/js/uaredirect.js" type="text/javascript"></script>
<script type="text/javascript">
    uaredirect("http://m.XXXX.com","http://www.XXXXX.com");
</script>

注意:这里需要将域名修改成你自己的域名

uaredirect.js的原文件是通过压缩的为了修改方便我已经将其格式化了一下。
源文件地址http://siteapp.baidu.com/static/webappservice/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
            }
        }
    }
};

接下来需要注意的问题来了,以上代码实现了不同版本之间自动跳转。但是如果我需要使用手机访问电脑版怎么办。

  1. 首先我们可以利用代码中的 if (!c.match(“fromapp”)) 这个判断,在手机强行访问电脑版的时候在url地址后面加上 #fromapp (如:www.xxxx.com#fromapp)。通过这个可以让自动跳转失效。
  2. 在url后面加上fromapp过后新的问题又出现了,通过手机浏览器访问电脑版的首页可以,但是如果我打开了新的连接再返回到主页,又会自动跳转到触屏版。这样的体验非常不好。
    我就对其代码做了一定的修改,在原有的代码基础上加入了cookie存储状态。

    if (b) {
            var c = window.location.hash;
            if (c.match("m")) {
                addCookie("ism","1", {expires: 60 * 60, path: ""});
            }else{
                var ism = getCookie("ism");
                if ($.trim(ism) == "" || ism != "1") {
                    if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i))) {
                        location.replace(f);
                    }
                }
            }
    
        }

    获取cookie和增加cookie的代码

    // 添加Cookie
    function addCookie(name, value, options) {
        if (arguments.length > 1 && name != null) {
            if (options == null) {
                options = {};
            }
            if (value == null) {
                options.expires = -1;
            }
            if (typeof options.expires == "number") {
                var time = options.expires;
                var expires = options.expires = new Date();
                expires.setTime(expires.getTime() + time * 1000);
            }
            if (options.path == null) {
                options.path = "/";
            }
            if (options.domain == null) {
                options.domain = "";
            }
            document.cookie = encodeURIComponent(String(name)) + "=" + encodeURIComponent(String(value)) + (options.expires != null ? "; expires=" + options.expires.toUTCString() : "") + (options.path != "" ? "; path=" + options.path : "") + (options.domain != "" ? "; domain=" + options.domain : "") + (options.secure != null ? "; secure" : "");
        }
    }
    
    // 获取Cookie
    function getCookie(name) {
        if (name != null) {
            var value = new RegExp("(?:^|; )" + encodeURIComponent(String(name)) + "=([^;]*)").exec(document.cookie);
            return value ? decodeURIComponent(value[1]) : null;
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值