文章摘要
这篇文章介绍了通过JavaScript实现的URL重写技术,旨在根据当前访问设备的类型(手机端或PC端)自动调整页面URL。代码通过检测用户设备的访问来源,判断是否是在手机网络环境下访问网页,如果是,则将URL从http://www.改为http://3g.,以确保在手机用户在3G网络下能够正确访问网页。这种技术有助于提升网站在不同设备环境下的兼容性和用户体验。
<script type="text/javascript"> (function(Switch){ var switch_pc = window.location.hash; var curURL = document.location.href; //当前URL var isMobile = curURL.indexOf("http://3g."); //判断当前URL是否是手机站 var isPc = curURL.indexOf("http://www."); //判断当前URL是否包含"http://www." if (isMobile < 0) { //不是手机站 if (isPc < 0) { //不包含"http://www." var thisURL = curURL.replace(/^http:\/\//,"http://3g."); } else { // 包含"http://www." var thisURL = curURL.replace(/^http:\/\/www\./,"http://3g."); } } if(switch_pc != "#pc"){ if(/iphone|ipod|ipad|ipad|Android|nokia|blackberry|webos|webos|webmate|bada|lg|ucweb|skyfire|sony|ericsson|mot|samsung|sgh|lg|philips|panasonic|alcatel|lenovo|cldc|midp|wap|mobile/i.test(navigator.userAgent.toLowerCase())){ Switch.location.href = thisURL ; }[xss_clean]('<meta name="mobile-agent" content="format=html5;url='+thisURL+'" />'); } })(window); </script>