5ucms论坛

标题: 百度判断手机终端并自动跳转uaredirect.js连接代码失效的解决办法 [打印本页]

作者: admin    时间: 2017-6-6 21:16
标题: 百度判断手机终端并自动跳转uaredirect.js连接代码失效的解决办法
现在很多人还对PC端内页跳转到手机端的指定内页,用百度的一个js代码挺好用,如下:
1、先要在PC端头部做PC端内页地址要跳转到哪个手机端的页面的定义,如下:
<meta name=”mobile-agent” content=”format=html5;url=http://m.yjcom.com/$shipei”>
说明:$shipei是变量,自己写。如果您就一个页面,就指定到具体页面即可。
2、使用js跳转,如下:

这里的http://siteapp.baidu.com/static/webappservice/uaredirect.js 已经失效。
您可以把这个js代码下载到本地。
uaredirect.js里的具体代码如下:
  1. function uaredirect(f) {
  2.     try {
  3.         if (document.getElementById("bdmark") != null) {
  4.             return
  5.         }
  6.         var b = false;
  7.         if (arguments[1]) {
  8.             var e = window.location.host;
  9.             var a = window.location.href;
  10.             if (isSubdomain(arguments[1], e) == 1) {
  11.                 f = f + "/#m/" + a;
  12.                 b = true
  13.             } else {
  14.                 if (isSubdomain(arguments[1], e) == 2) {
  15.                     f = f + "/#m/" + a;
  16.                     b = true
  17.                 } else {
  18.                     f = a;
  19.                     b = false
  20.                 }
  21.             }
  22.         } else {
  23.             b = true
  24.         }
  25.         if (b) {
  26.             var c = window.location.hash;
  27.             if (!c.match("fromapp")) {
  28.                 if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i))) {
  29.                     location.replace(f)
  30.                 }
  31.             }
  32.         }
  33.     } catch(d) {}
  34. }
  35. function isSubdomain(c, d) {
  36.     this.getdomain = function(f) {
  37.         var e = f.indexOf("://");
  38.         if (e > 0) {
  39.             var h = f.substr(e + 3)
  40.         } else {
  41.             var h = f
  42.         }
  43.         var g = /^www./;
  44.         if (g.test(h)) {
  45.             h = h.substr(4)
  46.         }
  47.         return h
  48.     };
  49.     if (c == d) {
  50.         return 1
  51.     } else {
  52.         var c = this.getdomain(c);
  53.         var b = this.getdomain(d);
  54.         if (c == b) {
  55.             return 1
  56.         } else {
  57.             c = c.replace(".", "\.");
  58.             var a = new RegExp("\." + c + "$");
  59.             if (b.match(a)) {
  60.                 return 2
  61.             } else {
  62.                 return 0
  63.             }
  64.         }
  65.     }
  66. };
复制代码


压缩后的代码如下:
  1. 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}}}};
复制代码





欢迎光临 5ucms论坛 (http://bbs.5ucms.com/) Powered by Discuz! X3.2