码迷,mamicode.com
首页 > 其他好文 > 详细

cookie操作简单实现

时间:2016-12-19 19:13:26      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:expires   com   cat   host   domain   ret   str   toe   key   

var Cookie = {
    get:function(key){
        var reg = new RegExp(‘^‘ + key + ‘=([^;]+)(?=;|$)‘,‘gi‘);
        return (result = document.cookie.match(reg)) == null ? null : decodeURICompoent(result[1]);
    },
    set:function(key,value,expireHours,path,domain,secure){
        var path = path || ‘/‘,
            domain = domain || location.host,
            cookie = [key + ‘=‘ + encodeURICompoent(value),‘path=‘ + path,‘domain=‘+domain];
        if(expire){
            cookie.push(‘expires=‘ + this.hoursToExpire(expireHours))
        }
        if(secure){
            cookie.push(secure)
        }
        document.cookie = cookie.join(‘‘);
        return document.cookie;
    },
    del:function(key){
        if(this.get(key)){var expire = new Date();
            expire.setTime(now.getTime() - 10000);
            this.set(key,‘‘,expire,path,domain);
        }
    },
    hoursToExpire:function(hours){
        if(parseInt(hours) == NaN){
            return ‘‘;
        }
        var now = new Date();
        now.setTime(now.getTime() + parseInt(hours)*60*60*1000);
        return now.toGMTString();
    }
}

 

cookie操作简单实现

标签:expires   com   cat   host   domain   ret   str   toe   key   

原文地址:http://www.cnblogs.com/mengff/p/6198309.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!