查看: 1842|回复: 0

JS写的cookie类

[复制链接]
发表于 2005-5-30 20:12:25 | 显示全部楼层 |阅读模式
&lt;script language="Javacript"&gt;<BR>function Cookie(delim){<BR>    this._Cookie=[];<BR>    this.Load=function(){<BR>        if(document.cookie.indexOf(";")!=-1){<BR>            var _sp,_name,_tp,_tars,_tarslength;<BR>            var _item=document.cookie.split("; ");<BR>            var _itemlength=_item.length;<BR>            while(_itemlength&gt;0){<BR>                _sp=_item[--_itemlength].split("=");<BR>                _name=_sp[0];<BR>                _tp=_sp[1].split(",");<BR>                _tars=_tp.slice(1,_tp.length);<BR>                this._Cookie[_name]=[];<BR>                this._Cookie[_name]=_tars;<BR>                this._Cookie[_name]["timeout"]=_tp[0];<BR>            }<BR>            return true;<BR>        }<BR>        return false;<BR>    }<BR>    this.Save=function(){<BR>        var _str,_ars,_mars,_marslength,timeout,i,key;<BR>        for(key in this._Cookie){<BR>            if(!this._Cookie[key])return;<BR>            _str=[];<BR>            _mars=CookieClass._Cookie[key];<BR>            _marslength=_mars.length;<BR>            for(i=0;i&lt;_marslength;i++)_str[_str.length]=escape(_mars);<BR>            document.cookie=key+"="+_mars["timeout"]+(_str.length&gt;0?",":"")+_str+(_mars["timeout"]==0?"":";expires="+new Date(parseInt(_mars["timeout"])).toGMTString());<BR>        }<BR>    }<BR>    this.GetCookieCount=function(){<BR>        var _length=0,key;<BR>        for(key in this._Cookie)_length++;<BR>        return _length;<BR>        }<BR>    this.Create=function(name,days){<BR>        days=days?days:0;<BR>        if(!this._Cookie[name])this._Cookie[name]=[];<BR>        this._Cookie[name]["timeout"]=days!=0?new Date().getTime()+parseInt(days)*86400000:0;<BR>        }<BR>    this.Modify=function(name,days){<BR>        this.Create(name,days);<BR>        }<BR>    this.GetTime=function(name){<BR>        return new Date(parseInt(this._Cookie[name]["timeout"]));<BR>        }<BR>    this.Delete=function(name){<BR>        this.Create(name,0);<BR>        }<BR>    this.AddItem=function(name,value){<BR>        this._Cookie[name][this._Cookie[name].length]=value;<BR>        }<BR>    this.DelItem=function(name,index){<BR>        var _ttime=this._Cookie[name]["timeout"];<BR>        this._Cookie[name]=this._Cookie[name].slice(0,index).concat(this._Cookie[name].slice(parseInt(index)+1,this._Cookie[name].length));<BR>        this._Cookie[name]["timeout"]=_ttime;<BR>        }<BR>    this.GetCount=function(name){<BR>        return this._Cookie[name].length;<BR>        }<BR>    this.GetItem=function(name,index){<BR>        return this._Cookie[name][index];<BR>        }<BR>    }<BR>&lt;/script&gt;<BR><BR>&lt;script language="Javascript"&gt;<BR>var CookieClass=new Cookie();<BR>if(!CookieClass.Load()){<BR>    CookieClass.Create("ass",1);<BR>    CookieClass.AddItem("ass","s1");<BR>    CookieClass.AddItem("ass","s2");<BR>    CookieClass.AddItem("ass","s3");<BR>    CookieClass.AddItem("ass","s4");<BR>    CookieClass.DelItem("ass",1);<BR>    CookieClass.Save();<BR>    }<BR>alert("Cookie过期时间:"+CookieClass.GetTime("Pass").toLocaleString());<BR>alert(document.cookie);<BR>&lt;/script&gt;<BR><BR>&lt;script language="Javascript"&gt;<BR>function eyunCookie(){<BR>    this.key="";//初始化key。<BR>    this.value="";//初始化key's value。<BR>    this.expires=0;//初始化cookie的有效时间,单位毫秒。<BR>    this.init=function(){//对象初始化<BR>        this.key="";<BR>        this.value="";<BR>        this.expires=0;<BR>    }<BR>    this.set=function(key,value,expires){//设置cookie<BR>        if(this.key=="")this.key=key;<BR>        if(this.value=="")this.value=value;<BR>        if(this.expires&lt;=0)this.expires=expires;<BR>        if(this.key==""||typeof(this.key)!="string"){<BR>            alert("请先设置欲保存的cookie名称!");<BR>            this.init();<BR>            return false;<BR>        }<BR>        if(this.key.match(/[,; ]/)){<BR>            alert("cookie名称中不能包含“,”、“;”或空格!");<BR>            this.init();<BR>            return false;<BR>        }<BR>        if(this.value.toString().match(/[,; ]/)||typeof(this.value)=="undefined"){<BR>            alert("cookie值中不能包含“,”、“;”或空格!");<BR>            this.init();<BR>            return false;<BR>        }<BR>        if(this.expires&lt;=0||typeof(this.expires)!="number"){<BR>            alert("请先正确设置cookie的有效时间!");<BR>            this.init();<BR>            return false;<BR>        }<BR>        var cookie=document.cookie;<BR>        if(cookie.indexOf(this.key+"=")!=-1){<BR>            if(!confirm("欲保存的cookie名称已经存在,是否要进行替换?")){<BR>                this.init();<BR>                return false;<BR>            }<BR>        }<BR>        var dt=new Date();<BR>        dt.setTime(dt.getTime()+this.expires);<BR>        document.cookie=this.key+"="+this.value+";expires="+dt.toGMTString();<BR>        this.init();<BR>        return true;<BR>    }<BR><BR>    this.get=function(key){//取得名为key的cookie的值<BR>        if(key==""||key.match(/[,; ]/)){<BR>            alert("请正确设置欲查找的cookie名称!")<BR>            return false;<BR>        }<BR>        var cookie=document.cookie;<BR>        var start=cookie.indexOf(key+"=");<BR>        if(start==-1){<BR>            alert("欲查找的cookie不存在!")<BR>            return false;<BR>        }<BR>        var end=cookie.indexOf(";",start);<BR>        if(end==-1)end=cookie.length;<BR>        var getCookie=cookie.substring(start+key.length+1,end);<BR>        alert("cookie:"+key+"的值为"+getCookie);<BR>        return getCookie;<BR>    }<BR>    this.showAll=function(){//显示所有cookie<BR>        alert("共有以下cookie对:\n"+document.cookie.split(";").toString().replace(/,/g,"\n"));<BR>    }<BR>    this.del=function(key){//删除名为key的cookie<BR>        if(key==""||key.match(/[,; ]/)){<BR>            alert("请正确设置欲删除的cookie名称!")<BR>            return false;<BR>        }<BR>        var dt=new Date();<BR>        dt.setTime(dt.getTime());<BR>        document.cookie=key+"=eyunDelete;expires="+dt.toGMTString();<BR>        this.init();<BR>        return true;<BR>    }<BR>    this.destroy=function(){//销毁所有cookie<BR>        var dt=new Date();<BR>        dt.setTime(dt.getTime());<BR>        while(document.cookie!=""){<BR>            document.cookie=document.cookie+";expires="+dt.toGMTString();<BR>            this.init();<BR>            return true;<BR>        }<BR>    }<BR>}<BR>var cookieTest=new eyunCookie()<BR>function settest(){<BR>    cookieTest.key="test"<BR>    cookieTest.value="ok"<BR>    cookieTest.expires=31536000000<BR>    cookieTest.set()<BR>}<BR>&lt;/script&gt;<BR><BR>&lt;input type=button onclick=cookieTest.showAll() value=read&gt;<BR>&lt;input type=button onclick="cookieTest.set('a','test',31536000000)" value=setA&gt;<BR>&lt;input type=button onclick="settest();" value=setTest&gt;<BR>&lt;input type=button onclick="cookieTest.destroy()" value=clear&gt;<BR>&lt;input type=button onclick=cookieTest.get("test") value=gettest&gt;<BR>&lt;input type=button onclick=cookieTest.get("a") value=geta&gt;<BR>&lt;input type=button onclick=cookieTest.set("test",1,31536000000) value=resetTest&gt;<BR>&lt;input type=button onclick=cookieTest.del("test") value=delTest&gt;<BR>
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条


关注公众号

相关侵权、举报、投诉及建议等,请发 E-mail:admin@discuz.vip

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖QQ客服返回顶部