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

相关文章:

  • 2022-12-23
  • 2022-02-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-04
猜你喜欢
  • 2021-09-30
  • 2022-02-21
  • 2021-09-22
  • 2022-12-23
  • 2021-09-30
  • 2021-12-29
相关资源
相似解决方案