百度判断手机终端并自动跳转js代码
百度目前为站长提供了判断手机终端类型并自动实现跳转的js脚本,极大的方便了广大站长及web开发人员。其js脚本的使用方法极其简单。代码:<script src="http://siteapp.baidu.com/static/webappservice/uaredirect.js" type="text/javascript"></script>
<SCRIPT type=text/javascript>uaredirect("手机站","WEB站");</SCRIPT>核心的js脚本格式化代码如下所示:function uaredirect(f) {
try {
if (document.getElementById("bdmark") != null) {
return
}
var b = false;
if (arguments) {
var e = window.location.host;
var a = window.location.href;
if (isSubdomain(arguments, e) == 1) {
f = f + "/#m/" + a;
b = true
} else {
if (isSubdomain(arguments, 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){var e=window.location.host;var a=window.location.href;if(isSubdomain(arguments,e)==1){f=f+"/#m/"+a;b=true}else{if(isSubdomain(arguments,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}}}}; 这下百度要收录js了!
这个得看看哈
页:
[1]