JS判断访问设备是否来自移动端还是web端

<script type="text/javascript">
(function(){
    if(document.location.href.indexOf('a.com.cn')>-1){
        setCookie("web_version","web",3600*3,'.com.cn');
    }
    var ua = navigator.userAgent.toLowerCase();
    function setCookie(name,value,expires,path,domain,secure){
        var cookieText = encodeURIComponent(name) + "=" + encodeURIComponent(value);
        if(expires instanceof Date){
            cookieText += ";expires=" + expires.toGMTString();
        }
        if(path){
            cookieText += ";path=" + path;
        }
        if(domain){
            cookieText += ";domain=" + domain;
        }
        if(secure){
            cookieText += ";secure";
        }
        document.cookie = cookieText;
    }
    function unset(name,path,domain,secure){
        setCookie(name,"",new Date(0),path,domain,secure);
    }
    function getCookie(name){
        var cookieName  = encodeURIComponent(name) + "=",
            cookieStart = document.cookie.indexOf(cookieName),
            cookieValue = null;
        if(cookieStart>-1){
            var cookieEnd = document.cookie.indexOf(";",cookieStart);
            if(cookieEnd == -1){
                cookieEnd = document.cookie.length;
            }
            cookieValue = decodeURIComponent(document.cookie.substring(cookieStart + cookieName.length,cookieEnd));
        }
        return cookieValue;
    }
    var uaLower = ua.toLocaleLowerCase();
    if( (uaLower.indexOf('android') > -1) || ( uaLower.indexOf('iphone') > -1) || ( uaLower.indexOf('ipad') > -1) ||( uaLower.indexOf('moblie') > -1) ){
        if(false){
        }else{
            var jump = getCookie("web_version");
            if(jump == null){
                window.location = 'http://a.com.cn';
                setCookie("web_version","web",3600*3,'.com.cn');
            }else{
                if(jump == "wap"){
                    window.location = 'http://a.com.cn';
                }else if(jump == "web"){
                   // window.location = 'http://a.com.cn';//什么都不做 留在当前页
                }
            }
        }
    }
})();
</script>


你可能感兴趣的:(JS获取访问该页面的设备)