zoukankan      html  css  js  c++  java
  • 利用百度uaredirect.js判断手机终端并自动跳转

    百度siteapp下的一款跳转的产品,使用起来很方便,你可以用这款JS判断是否手机终端访问。

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

    头页面引用:

    <script src="./uaredirect.js" type="text/javascript"></script>
    <!-- m.xxx.com 为移动端,www.xxx.com 为PC端 -->
    <script type="text/javascript">uaredirect("http://m.xxx.com","http://www.xxx.com");</script>
  • 相关阅读:
    linux 内存映射-ioremap和mmap函数
    vue 模板语法-插值的操作(12-22)
    chrome浏览器json插件
    vue初识(1-11)2020-10-27
    后盾人:JS课程第一章(11-18)2020-10-25
    博客园美化
    chrome 设置自动刷新网页
    二叉树的层次遍历
    poj 2905 双向队列(待补充)
    poj 2159 D
  • 原文地址:https://www.cnblogs.com/tangxuliang/p/9078964.html
Copyright © 2011-2022 走看看