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>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章