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;
    }
}
相關文章
相關標籤/搜索