uaredirect手机访问网站转向手机网站代码

虽然现在很多网站已经采用了多平台自适应的网站,但是也还是有一些网站采用PC端和移动手机站分开的方式,这就需要在客户通过手机访问网站的时候,将访问引导到手机站,以便客户可以获得更好的访问体验,例如我们想将https://www.chinaobd2.com 这个站,当客户通过手机访问的时候将域名转向到https://m.chinaobd2.com. 通过什么代码来达到最好的效果呢。

 

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}}}};
var location_url = 'https://m.chinaobd2.com';
var location_host = window.location.pathname;
var newUrl = location_url+location_host;
uaredirect(newUrl);

  当然,还有其他方法,也比较简单,通过js判断屏幕大小来实现转向,如果屏幕小于728 就转向到手机站。

 

posted @ 2021-12-18 10:58  hilong911  阅读(112)  评论(0编辑  收藏  举报