检测用户浏览器和屏幕分别率


html页面
 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script src="js/script.js"></script>
</body>
</html>
 
 
 
script.js页面
 
;(function(win,doc){
 
    var nav=navigator,
        scr=screen,
        ua=nav.userAgent;
    //判断浏览器
 
    var bowser={
        //如果是iOS浏览器
        isIos:function(){
             if(ua.match(/iPhone/i) || ua.match(/iPad/i) || ua.match(/iPod/i)){
                 return true;
             }else{
                 return false;
             }
        },
        //如果是安卓浏览器
        isAndroid:function(){
            if(ua.match(/Android/i)){
                return true;
            }else{
                return false;
            }
        },
        //如果是PC 浏览器
        isPc:function(){
            if(!this.isIos() && !this.isAndroid()){
                return true;
            }else{
                false
            }
            //if(!ua.match(/Android/i) && !ua.match(/iPhone/i) && )
        },
        // 检测浏览器分别率
        getScreen:function(){
            return scr.width+'*'+scr.height;
        }
    },  cookie封装
    ckFn={
        setCookie:function(name,value,days){
            //如果不设置天数 , 默认为30天
            days=days?days:30;
            var exp=new Date();
            exp.setTime(exp*1+days*(24*60*60*1000));
            if(typeof(Domain)==="undefined" || Domain==null){
                Domain=location.host // 获取主机地址
            }
            doc.cookie=name+'='+escape(value)+';expires='+exp+';domain='+Domain+';path=/';
        },
        getCookie:function(name){
            // document.cookie 获取所有的cookie 包括名称和值 类型为字符串
            var reg=new RegExp('(^| )'+name+'=([^;]*)(;|$)');
            var arr=doc.cookie.match(reg);
            if(arr) return unescape(arr[2]);
            return null;
        },
        delCokie:function(name){
            // 设置时间过期
            if(getCookie(name)){
                setCookie(name,"",-1);
            }
        }
    }
 
 
    var CI=function(){
        return {
            bowser:bowser,
            ck:ckFn
 
        }
    }
 
 
    win.CI=CI;
 
})(window,document);
 
//console.log(CI.bowser.isIos());
 
console.log(CI().bowser.isAndroid())
 
console.log(CI().bowser.getScreen())
 
CI().ck.setCookie("lili","marry@163.com",1);
console.log(CI().ck.getCookie("lili"));
posted @ 2016-12-20 17:07  *小七儿*  阅读(380)  评论(0编辑  收藏  举报