百度判断手机终端并自动跳转uaredirect.js连接代码失效的解决办法
现在很多人还对PC端内页跳转到手机端的指定内页,用百度的一个js代码挺好用,如下:
1、先要在PC端头部做PC端内页地址要跳转到哪个手机端的页面的定义,如下:
<meta name=”mobile-agent” content=”format=html5;url=http://m.zhzx.info/$shipei”>
说明:$shipei是变量,自己写。如果您就一个页面,就指定到具体页面即可。
2、使用js跳转,如下:
<script src=”http://siteapp.baidu.com/static/webappservice/uaredirect.js” type=”text/javascript”></script>
<script type=”text/javascript”>uaredirect(window.location.href.replace(”www.”,”m.”));</script>
这里的http://siteapp.baidu.com/static/webappservice/uaredirect.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 } } }};
压缩后的代码如下:
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}}}};
点击这里下载JS文件:uaredirect.js