晓东哥
人生就是一道命题,每个人的答案可能不尽相同,但你的内容中如果完全被物欲和世俗充斥,那你得到的一定不会是最好的结局。 -------晓东

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