javascript 设置读取Cookie

/*
    * 获取cookie
    * */
    getCookie: function(name){
        var str = '';
        var cookies = document.cookie;
        var arrCookie = cookies.split(';');
        for(var i=0;i<arrCookie.length;i++){
            var content = arrCookie[i].split('=');
            if(content[0].replace(/^\s+|\s+$/g,"") == name.replace(/^\s+|\s+$/g,"")){
                str = decodeURI(content[1]);
                break;
            }
        }
        return str;
    },

    /*
    * 写cookie
    * expiresHours 多少天过期
    * */
    setCookie: function(name,value,expiresHours,path){
        if(cpm.isEmpty(path))path = '/';
        var cookieString = name + "=" + encodeURI(value) + '; path='+path;
        if(expiresHours > 0){
            var date = new Date();
            date.setTime(date.getTime() + expiresHours * 86400);
            cookieString = cookieString + "; expires=" + date.toGMTString();
        }
        document.cookie = cookieString;
    },

 记录一下,下次要用的时候就不用重新写了

posted @ 2015-01-15 09:24  树下的枫  阅读(130)  评论(0编辑  收藏  举报