javascript cookie 操做

贴代码javascript

var cookies = {
    d: new Date(),
    days: function(d){
        return parseInt(d)*24*3600*1000;
    },
    iniCookie: function(val){//初始化Cookie操做
        if(val === 'add'){
            this.d.setTime(this.d.getTime()+this.days(2));//设置存活时间2表示2天
        } else if(val === 'del'){
            this.d.setTime(this.d.getTime()-this.days(1));//提早时间销毁cookie
        }
        this.d = this.d.toGMTString();
    },
    getCookie: function(name){//获取Cookie操做
        var arr = document.cookie.split(';');
        for(var x in arr){
            var arr2 = arr[x].split('=');
            //得到cookie值
            if(arr2[0] === name){
                return arr2[1];
            }
            // if(arr2[0] === 'left_active'){
            //  return arr2[1];
            // }
        }
    },
    setCookie: function(name){//设置Cookie操做
        this.iniCookie('add');
        document.cookie="left_active="+name+";expires="+this.d;
    },
    delCookie: function(name){//删除Cookie操做
        this.iniCookie('del');
        document.cookie="left_active="+name+";expires="+this.d;
    },
    showCookie: function(){//显示全部Cookie操做
        return document.cookie;
    }
}
相关文章
相关标签/搜索