返回顶部

收藏

用js控制cookie(cookie操作类)

更多
function cookie(name, value, expires, path, domain, secure) {
    // 初始化
    this.name = name;
    this.value = value;
    this.expires = expires;
    this.path = path;
    this.domain = domain;
    this.secure = secure;

    // 读取cookie
    this.read = function () {
        // 快捷操作
        var ck = document.cookie;

        var arg = this.name + "=";
        var alen = arg.length;
        var clen = ck.length;
        var i = 0;
        while (i < clen) {
            var j = i + alen;
            if (ck.substring(i, j) == arg) {
                var endstr = ck.indexOf(";", j);
                if (endstr == -1)
                    endstr = ck.length;
                return unescape(ck.substring(j, endstr));
            }
            i = ck.indexOf(" ", i) + 1;
            if (i == 0)
                break;
        }
        return null;
    }

    // 设置cookie
    this.set = function () {
        // 更新cookie过期时间
        expStore = this.expires;

        exp = new Date();
        base = new Date(0);
        skew = base.getTime();
        if (skew > 0)
            exp.setTime(exp.getTime() - skew);
        exp.setTime(exp.getTime() + (this.expires * 24 * 60 * 60 * 1000));
        this.expires = exp;

        document.cookie = this.name + "=" + escape(this.value) +
            ((this.expires) ? "; expires=" + this.expires.toGMTString() : "") +
            ((this.path) ? "; path=" + this.path : "") +
            ((this.domain) ? "; domain=" + this.domain : "") +
            ((this.secure) ? "; secure" : "");

        // 刷新cookie类中的cookie过期时间
        this.expires = expStore;
    }

    // 删除cookie
    this.kill = function () {
        document.cookie = this.name + "=" +
            ((this.path) ? "; path=" + this.path : "") +
            ((this.domain) ? "; domain=" + this.domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }

    // 更改cookie类中的cookie属性
    this.changeName = function (chName) {
        this.kill();
        this.name = chName;
        this.set();
    }
    this.changeVal = function (chVal) {
        this.kill();
        this.value = chVal;
        this.set();
    }
    this.changeExp = function (chExp) {
        this.kill();
        this.expires = chExp;
        this.set();
    }
    this.changePath = function (chPath) {
        this.kill();
        this.path = chPath;
        this.set();
    }
    this.changeDomain = function (chDom) {
        this.kill();
        this.domain = chDom;
        this.set();
    }
    this.changeSecurity = function (chSec) {
        this.kill();
        this.secure = chSec;
        this.set();
    }
}

标签:javascript

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. K-Res 发表 2018-10-27 12:10:11 关于Javascript中类成员函数中内嵌函数的this闭包问题
  2. 杨魁 发表 2018-10-25 07:28:13 Under the Hood: NaN of JS
  3. 尖兵 发表 2018-10-25 11:03:47 服务重启导致的Java服务抖动CPU占用高
  4. 可乐加糖 发表 2018-10-23 08:31:16 JavaScript文档生成器JSDuck
  5. 可乐加糖 发表 2018-10-23 08:31:16 JavaScript文档生成器JSDuck
  6. wenming.gapo 发表 2018-10-23 11:32:30 AOP装饰函数与小T的情愫(二)
  7. 尖兵 发表 2018-10-18 13:19:02 Java诊断工具Arthas
  8. 尖兵 发表 2018-10-18 13:19:02 Java诊断工具Arthas
  9. hellas 发表 2018-10-18 14:20:31 AOP装饰函数与小T的情愫
  10. 博主 发表 2018-10-15 10:02:38 生成聚合收款二维码:支付宝、微信、QQ 钱包
  11. axiu 发表 2018-10-14 02:30:40 微信小程序开发总结
  12. xirruiqiang 发表 2018-10-14 08:14:43 round 函数在不同语言中的实现乱象

发表评论