常用代码之手机访问pc网站自动跳转手机端网站代码

网站建设2年前 (2022)发布 admin
28 0

亲测简单可用

手机访问PC网站自动跳转手机端网站PHP代码

代码1(在浏览器模拟的手机环境中,未执行跳转)

<script>
if(navigator.platform.indexOf(‘Win32’)!=-1){
//window.location.href=”http://1.laiyuehui.cn/003/index.html”;
}else{
window.location.href=”http://1.laiyuehui.cn/003/m/index.html”;
}
</script>

代码2;手机或电脑模拟手机环境访问的时候,自动跳转

<script type=”text/javascript”>
// JavaScript Document
function urlredirect() {
var sUserAgent = navigator.userAgent.toLowerCase();
if ((sUserAgent.match(/(ipod|iphone os|midp|ucweb|android|windows ce|windows mobile)/i))) {
// PC跳转移动端
var thisUrl = window.location.href;
window.location.href = thisUrl.substr(0,thisUrl.lastIndexOf(‘/’)+1)+’mobile/’;
}
}
urlredirect();
</script>

代码3

<script language=”JavaScript”>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}}}};uaredirect(“m.your-website.com”);</script>

© 版权声明

相关文章

暂无评论

暂无评论...