Home > Web Front-end > JS Tutorial > body text

Baidu determines the mobile terminal and automatically jumps to js code and usage examples_javascript skills

WBOY
Release: 2016-05-16 16:44:52
Original
1207 people have browsed it

Baidu currently provides webmasters with a js script that determines the type of mobile phone terminal and automatically implements the jump, which greatly facilitates the majority of webmasters and web developers. The use of its js script is extremely simple.

Copy code The code is as follows:




The most core js script formatting code is as follows:
Copy codeThe code is as follows:

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

建议站长及web开发人员使用如下压缩后的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}}}};
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template