例一是打开首页判断进行跳转;例二是打开任何页面判断客户端并重写url。
一、
<script type="text/javascript"> var mobileAgent = new Array("Windows Phone","iphone", "ipod", "ipad", "android", "mobile", "blackberry","SymbianOS", "webos", "incognito", "webmate", "bada", "nokia", "lg", "ucweb", "skyfire"); var browser = navigator.userAgent.toLowerCase(); for (var i=0; i<mobileAgent.length; i++){ if (browser.indexOf(mobileAgent[i])!=-1){ location.href = http://m.dsj365.cn"; break; } } </script>
二、
<script type="text/javascript"> (function(){ var reWriteUrl = function(url){ if(url){ var Splits = url.split("/"),siteName=window.location.pathname; if(typeof siteName!=="undefined"){ return "http://m.dsj365.cn"+siteName; } } }; if(/Android|webOS|iPhone|iPad|Windows Phone|iPod|BlackBerry|SymbianOS|Nokia|Mobile/i.test(navigator.userAgent)){ var url=window.location.href; var pathname=window.location.pathname; if(url.indexOf("?mobile")<0){ try{ window.location.href=reWriteUrl(url); }catch(e){} } }; })(); </script>
注意,如果给搜索引擎看的话我们在文件头加入
<meta http-equiv="mobile-agent" content="format=xhtml; url=http://m.dsj365.cn" /> <meta http-equiv="mobile-agent" content="format=html5; url=http://m.dsj365.cn" />
意思是告诉搜索引擎移动蜘蛛抓取的是手机版本的.
扫码关注