JS写的Cookie类

该博客主要展示了使用JavaScript实现Cookie操作的代码。定义了Cookie类,包含加载、保存、创建、修改、删除等方法,还定义了eyunCookie类,可进行设置、获取、删除等操作,通过代码示例演示了如何使用这些类来管理Cookie。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1<scriptlanguage="JScript">
2functionCookie(delim){
3this._Cookie=[];
4this.Load=function(){
5if(document.cookie.indexOf(";")!=-1){
6var_sp,_name,_tp,_tars,_tarslength;
7var_item=document.cookie.split(";");
8var_itemlength=_item.length;
9while(_itemlength>0){
10_sp=_item[--_itemlength].split("=");
11_name=_sp[0];
12_tp=_sp[1].split(",");
13_tars=_tp.slice(1,_tp.length);
14this._Cookie[_name]=[];
15this._Cookie[_name]=_tars;
16this._Cookie[_name]["timeout"]=_tp[0];
17}
18returntrue;
19}
20returnfalse;
21}
22this.Save=function(){
23var_str,_ars,_mars,_marslength,timeout,i,key;
24for(keyinthis._Cookie){
25if(!this._Cookie[key])return;
26_str=[];
27_mars=CookieClass._Cookie[key];
28_marslength=_mars.length;
29for(i=0;i<_marslength;i++)_str[_str.length]=escape(_mars[i]);
30document.cookie=key+"="+_mars["timeout"]+(_str.length>0?",":"")+_str+(_mars["timeout"]==0?"":";expires="+newDate(parseInt(_mars["timeout"])).toGMTString());
31}
32
33}
34this.GetCookieCount=function(){
35var_length=0,key;
36for(keyinthis._Cookie)_length++;
37return_length;
38}
39this.Create=function(name,days){
40days=days?days:0;
41if(!this._Cookie[name])this._Cookie[name]=[];
42this._Cookie[name]["timeout"]=days!=0?newDate().getTime()+parseInt(days)*86400000:0;
43}
44this.Modify=function(name,days){
45this.Create(name,days);
46}
47this.GetTime=function(name){
48returnnewDate(parseInt(this._Cookie[name]["timeout"]));
49}
50this.Delete=function(name){
51this.Create(name,0);
52}
53this.AddItem=function(name,value){
54this._Cookie[name][this._Cookie[name].length]=value;
55}
56this.DelItem=function(name,index){
57var_ttime=this._Cookie[name]["timeout"];
58this._Cookie[name]=this._Cookie[name].slice(0,index).concat(this._Cookie[name].slice(parseInt(index)+1,this._Cookie[name].length));
59this._Cookie[name]["timeout"]=_ttime;
60}
61this.GetCount=function(name){
62returnthis._Cookie[name].length;
63}
64this.GetItem=function(name,index){
65returnthis._Cookie[name][index];
66}
67}
68</script>
69<scriptlanguage="JScript">
70varCookieClass=newCookie();
71if(!CookieClass.Load()){
72CookieClass.Create("Pass",1);
73CookieClass.AddItem("Pass","Ps1");
74CookieClass.AddItem("Pass","Ps2");
75CookieClass.AddItem("Pass","Ps3");
76CookieClass.AddItem("Pass","Ps4");
77CookieClass.DelItem("Pass",1);
78CookieClass.Save();
79}
80alert("Cookie过期时间:"+CookieClass.GetTime("Pass").toLocaleString());
81alert(document.cookie);
82</script>
83<script>
84functioneyunCookie()
85{this.key="";//初始化key。
86this.value="";//初始化key'svalue。
87this.expires=0;//初始化cookie的有效时间,单位毫秒。
88this.init=function()//对象初始化
89{this.key="";
90this.value="";
91this.expires=0;
92}
93this.set=function(key,value,expires)//设置cookie
94{if(this.key=="")this.key=key;
95if(this.value=="")this.value=value;
96if(this.expires<=0)this.expires=expires;
97if(this.key==""||typeof(this.key)!="string")
98{alert("请先设置欲保存的cookie名称!");
99this.init();
100returnfalse;
101}
102if(this.key.match(/[,;]/))
103{alert("cookie名称中不能包含“,”、“;”或空格!");
104this.init();
105returnfalse;
106}
107if(this.value.toString().match(/[,;]/)||typeof(this.value)=="undefined")
108{alert("cookie值中不能包含“,”、“;”或空格!");
109this.init();
110returnfalse;
111}
112if(this.expires<=0||typeof(this.expires)!="number")
113{alert("请先正确设置cookie的有效时间!");
114this.init();
115returnfalse;
116}
117varcookie=document.cookie;
118if(cookie.indexOf(this.key+"=")!=-1)
119{if(!confirm("欲保存的cookie名称已经存在,是否要进行替换?"))
120{this.init();
121returnfalse;
122}
123}
124vardt=newDate();
125dt.setTime(dt.getTime()+this.expires);
126document.cookie=this.key+"="+this.value+";expires="+dt.toGMTString();
127this.init();
128returntrue;
129}
130this.get=function(key)//取得名为key的cookie的值
131{if(key==""||key.match(/[,;]/))
132{alert("请正确设置欲查找的cookie名称!")
133returnfalse;
134}
135varcookie=document.cookie;
136varstart=cookie.indexOf(key+"=");
137if(start==-1)
138{alert("欲查找的cookie不存在!")
139returnfalse;
140}
141varend=cookie.indexOf(";",start);
142if(end==-1)
143end=cookie.length;
144vargetCookie=cookie.substring(start+key.length+1,end);
145alert("cookie:"+key+"的值为"+getCookie);
146returngetCookie;
147}
148this.showAll=function(){alert("共有以下cookie对:\n"+document.cookie.split(";").toString().replace(/,/g,"\n"));}//显示所有cookie
149this.del=function(key)//删除名为key的cookie
150{if(key==""||key.match(/[,;]/))
151{alert("请正确设置欲删除的cookie名称!")
152returnfalse;
153}
154vardt=newDate();
155dt.setTime(dt.getTime());
156document.cookie=key+"=eyunDelete;expires="+dt.toGMTString();
157this.init();
158returntrue;
159}
160this.destroy=function()//销毁所有cookie
161{vardt=newDate();
162dt.setTime(dt.getTime());
163while(document.cookie!="")
164document.cookie=document.cookie+";expires="+dt.toGMTString();
165this.init();
166returntrue
167}
168}
169varcookieTest=neweyunCookie()
170
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值