返回顶部

收藏

用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. cc 发表 2018-03-20 14:36:25 canvas小tip–下载二维码、图片加水印
  2. 阮一峰 发表 2018-03-20 07:11:48 Node 调试工具入门教程
  3. Flying 发表 2015-10-20 11:26:46 Vue.js中插槽的使用
  4. cc 发表 2018-03-19 14:33:06 分享一个生成二维码的插件–QRCode.js
  5. Flying 发表 2015-12-04 16:30:40 图解Vue.js的生命周期
  6. Flying 发表 2016-03-20 16:39:18 常见的Vue.js问题
  7. K-Res 发表 2018-03-18 13:46:04 DOMException: play() can only be initiated by a us
  8. Flying 发表 2016-08-17 02:29:35 Angular的变化
  9. c君不在 发表 2017-06-13 08:05:04 fetch
  10. c君不在 发表 2017-12-14 07:32:57 浏览器端 async/await 还是有点代价的
  11. 博主 发表 2018-03-16 09:57:17 如何使用 JavaScript 实现一门编程语言
  12. 博主 发表 2018-03-15 14:17:59 filterToQueryString - JavaScript function to turn

发表评论