用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - javascript代码库

用js控制cookie(cookie操作类)

2013-07-03 作者: 小蜜锋举报

[javascript]代码库

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();
    }
}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...