Web_JavaScript_检测访问设备,以实现移动设备访问时跳转到移动站点;

->检测脚本

var client = function(){
    var system = {
        win: false,
        mac: false,
		
        iphone: false,
        ipod: false,
        ipad: false,
        ios: false,
        android: false,
        nokiaN: false,
        winMobile: false
    };    
	
   	var ua = navigator.userAgent;    
    var p = navigator.platform;
    system.win = p.indexOf("Win") == 0;
    system.mac = p.indexOf("Mac") == 0;
	
    if (system.win){
        if (/Win(?:dows )?([^do]{2})\s?(\d+\.\d+)?/.test(ua)){
            if (RegExp["$1"] == "NT"){
                switch(RegExp["$2"]){
                    case "5.0":
                        system.win = "2000";
                        break;
                    case "5.1":
                        system.win = "XP";
                        break;
                    case "6.0":
                        system.win = "Vista";
                        break;
                    case "6.1":
                        system.win = "7";
                        break;
                    default:
                        system.win = "NT";
                        break;                
                }                            
            } else if (RegExp["$1"] == "9x"){
                system.win = "ME";
            } else {
                system.win = RegExp["$1"];
            }
        }
    }
    
    system.iphone = ua.indexOf("iPhone") > -1;
    system.ipod = ua.indexOf("iPod") > -1;
    system.ipad = ua.indexOf("iPad") > -1;
    system.nokiaN = ua.indexOf("NokiaN") > -1;
    
    if (system.win == "CE"){
        system.winMobile = system.win;
    } else if (system.win == "Ph"){
        if(/Windows Phone OS (\d+.\d+)/.test(ua)){;
            system.win = "Phone";
            system.winMobile = parseFloat(RegExp["$1"]);
        }
    }
    
    if (system.mac && ua.indexOf("Mobile") > -1){
        if (/CPU (?:iPhone )?OS (\d+_\d+)/.test(ua)){
            system.ios = parseFloat(RegExp.$1.replace("_", "."));
        } else {
            system.ios = 2;
        }
    }
    
    if (/Android (\d+\.\d+)/.test(ua)){
        system.android = parseFloat(RegExp.$1);
    }
	
    return {
		developer: "cyb_23",
        system: system
	};

}();



->跳转脚本


<script type="text/javascript">
    window.onload = function() {
        var c = client.system;
        if (c.iphone || c.ipod || c.ipad || c.nokiaN || c.winMobile != false || c.android != false || c.ios != false) {
            location.href = 'http://3g.yourdomain.com/';
        }
    }
</script>

通常来说,等页面加载完成后再检测,已经是失去先机了的!检测,就应该在最最开始之初,进行检测,而不是在用户都耐心等待加载所有页面元素后检测!



->改善后版本
var url = window.location.href;
var isPast = "";
if (url.indexOf("?") > -1) {
    var qstr = url.split("?")[1];
    isPast = qstr.split("=")[1];
}
if (isPast != "true") {
    var c = client.system;
    if (c.iphone || c.ipod || c.ipad || c.nokiaN || c.winMobile != false || c.android != false || c.ios != false) {
        location.href = 'http://www.yourdomain.com/frienddlyPage.html';
    }
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>温馨提示</title>
	<style type="text/css">
		div a {
			text-decoration:none;
			display:block;
			text-align:center;
			margin-top:10px;
		}
	</style>
	<script type="text/javascript">
		var timeid;
		var sp;
		var seconds = 5;
		function changeSecond() {
			sp.innerHTML = --seconds;
			if(seconds == 0) {
				clearInterval(timeid);
				window.location.href = "http://4g.yourdomain.com";
			}
		}
		window.onload = function() {
			sp = document.getElementById("second");
			timeid = setInterval("changeSecond()",1000);
		}
	</script>
</head>

<body>
	<div>
		<a href="http://4g.<span style="font-size: 13.3333339691162px; font-family: Arial, Helvetica, sans-serif;">yourdomain</span><span style="font-size: 12px; font-family: Arial, Helvetica, sans-serif;">.com">直接跳转到手机站点</a></span>
		<a><span id="second">5</span>秒后自动跳转</a>
		<a href="http://www.yourdomain.com?p=true">还是访问电脑站</a>
	</div>
</body>
</html>