随着智能手机和平板电脑等移动设备的大量普及,制作一个移动版的网站已经是一件迫在眉睫的事情。当我们用移动设备访问电脑版网站的时候,如何让它自动跳转到移动版呢?我们可以利用js里的navigator.userAgent来进行判断。具体代码如下:
function browserRedirect() {
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) {
if(document.referrer!="http://m.zhishidaren.com/") { //让用户可以在移动端访问电脑版
window.location="http://m.zhishidaren.com/";
}
}
}
browserRedirect();