zoukankan      html  css  js  c++  java
  • cookie操作简单实现

    var Cookie = {
        get:function(key){
            var reg = new RegExp('(?:^| )' + key + '=([^;]+)(?=;|$)','gi');
            return (result = document.cookie.match(reg)) == null ? null : decodeURIComponent(result[1]);
        },
        set:function(key,value,expireHours,path,domain,secure){
            var path = path || '/',
                domain = domain || location.host,
                cookie = [key + '=' + encodeURIComponent(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();
        }
    }
  • 相关阅读:
    学习进度笔记14
    学习进度笔记13
    学习进度笔记12
    学习进度笔记11
    学习进度笔记10
    学习进度笔记9
    学习进度笔记8
    学习进度笔记7
    学习进度笔记6
    微信客户端兼容性
  • 原文地址:https://www.cnblogs.com/mengff/p/6198309.html
Copyright © 2011-2022 走看看