一个 cookie 类

//-------------------------------------------------------------
//Author : tonton
//Email  : tonton@yeah.net
//Date   : 2004-07-28
//Warning: free to use for everyone, don't stand on ceremony
//-------------------------------------------------------------
if (typeof __$__js__io__cookie__class__$__ == "undefined"){
    var __$__js__io__cookie__class__$__="js.io.Cookie";
    function Cookie(Name, Value){
        _overload:
            this.toString=function(){return this.value;}
            this.valueOf=function(){return this.value;}
        _pubic:
            this.name=Name;
            this.value=Value;   
    }
    function Cookies(cookiePath, cookieDomain, cookieSecure){
        _private:
            var _path;
            var _domain;
            var _secure;
           
            var _tail;
        _overload:
            this.toString =function(){return "CookiesClass";};
            this.valueOf  =function(){return document.cookie;};
        _pubic:
            this.getPath=function(){return this._path;}
            this.getDomain=function(){return this._domain;}
            this.getSecure=function(){return this._secure;}
           
            this.items=null;   
           
            this.reset=function(Path, Domain, Secure){
                if (Path!=null) this._path=Path;
                else Path=this._path;
               
                if (Domain!=null) this._domain=Domain;
                else Domain=this._Domain;
               
                if (Secure!=null) this._secure=Secure;
                else Secure=this._secure;
               
                var s="";
                if (Path)   s+="path="    + Path + ";";
                if (Domain) s+="Domain="  + Domain + ";";
                if (Secure) s+="secure;";
               
                this._tail=s;
                this.getAll()
            }
           
            this.get=function(cookieName){
                if (cookieName){
                    if (this.items.length==0) this.getAll();
                    for (var i=0; i<this.items.length; i++)
                        if (this.items[i].name==cookieName)
                            return this.items[i];   
                }
            }   
            this.getAll=function(){
                var c=document.cookie;
                this.items=new Array();
                if (c==null) return null;
               
                c=c.split(";");
                var j=-1;
               
                for (var i=0; i<c.length; i++){
                    var t=c[i].indexOf("=");
                    if (t>-1){
                        var n=unescape(c[i].substring(0,t));
                        this.items[++j]=new Cookie(n, unescape(c[i].substring(t+1)));
                    }
                }
                return this.items;
            };
            this.set=function(Name, Value, Expire){
                if (!(Name && Value)) return false;
               
                if (!(Expire instanceof Date)){
                    if (typeof Expire == "number"){
                        var d=new Date();
                        d.setTime(d.getTime() + parseInt(Expire) * 1000);
                        Expire=d;
                    }else{
                        Expire=null;
                    }
                }
               
                var s=unescape(Name) + "=" + unescape(Value) + ";";
                if (Expire) s+="expires=" + Expire.toGMTString() + ";";
               
                document.cookie=s+this._tail;
                return true;
            }
            this.remove=function(Name){
                var d=new Date();
                d.setTime(d.getTime() - 1000);
                document.cookie=unescape(Name) + "=''; expires=" + d.toGMTString() + ";";
            }
        _initial:
            this.reset(cookiePath, cookieDomain, cookieSecure);
    }
}    //End of __$__js__io__cookie__class__$__
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值