电脑网站做了一段时间了,后来又做了网站的手机端。想让用户从搜索引擎搜索到的网站内容后,判定用户用的是手机端还是pc端,并做相应的跳转,下面以奇迹之城小草导航为例:
<script type="text/javascript">
var sUserAgent= navigator.userAgent.toLowerCase();
//判定用户使用的是什么浏览器
var bIsIpad= sUserAgent.match(/ipad/i) == "ipad";
var bIsIphoneOs= sUserAgent.match(/iphone os/i) == "iphone os";
var bIsMidp= sUserAgent.match(/midp/i) == "midp";
var bIsUc7= sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
var bIsUc= sUserAgent.match(/ucweb/i) == "ucweb";
var bIsAndroid= sUserAgent.match(/android/i) == "android";
var bIsCE= sUserAgent.match(/windows ce/i) == "windows ce";
var bIsWM= sUserAgent.match(/windows mobile/i) == "windows mobile";
if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {
//使用这些浏览器的认为是从手机端访问的,下面进行跳转处理,因为PC端网址是http://www.qjdcs.com/和手机端的网址是http://m.qjdcs.com/,区别是一个域名是www另外一个是m,所以只需把www替换成m就行了。
window.location.href=window.location.href.replace("www","m");
}
</script>