JavaScript-Written Cookie class

Source: Internet
Author: User
Tags date key split tostring valid
Cookie|javascript <script language= "JScript" >
function Cookie (Delim) {
This._cookie=[];
This. Load=function () {
if (Document.cookie.indexOf (";")! =-1) {
var _sp,_name,_tp,_tars,_tarslength;
var _item=document.cookie.split (";");
var _itemlength=_item.length;
while (_itemlength>0) {
_sp=_item[--_itemlength].split ("=");
_NAME=_SP[0];
_tp=_sp[1].split (",");
_tars=_tp.slice (1,_tp.length);
This._cookie[_name]=[];
This._cookie[_name]=_tars;
this._cookie[_name]["Timeout"]=_tp[0];
}
return true;
}
return false;
}
This. Save=function () {
var _str,_ars,_mars,_marslength,timeout,i,key;
For (key in This._cookie) {
if (!this._cookie[key]) return;
_str=[];
_mars=cookieclass._cookie[key];
_marslength=_mars.length;
for (i=0;i<_marslength;i++) _str[_str.length]=escape (_mars[i]);
document.cookie=key+ "=" +_mars["Timeout"]+ (_str.length>0? ",": ") +_str+ (_mars[" timeout "]==0?" ":" expires= "+ New Date (parseint (_mars["Timeout")). toGMTString ());
}

}
This. Getcookiecount=function () {
var _length=0,key;
For (key in This._cookie) _length++;
return _length;
}
This. Create=function (name,days) {
days=days?days:0;
if (!this._cookie[name]) this._cookie[name]=[];
this._cookie[name]["Timeout"]=days!=0?new Date () getTime () +parseint (days) *86400000:0;
}
This. Modify=function (name,days) {
This. Create (name,days);
}
This. Gettime=function (name) {
return new Date (parseint (this._cookie[name]["timeout"));
}
This. Delete=function (name) {
This. Create (name,0);
}
This. Additem=function (Name,value) {
This._cookie[name][this._cookie[name].length]=value;
}
This. Delitem=function (Name,index) {
var _ttime=this._cookie[name]["Timeout"];
This._cookie[name]=this._cookie[name].slice (0,index). Concat (This._cookie[name].slice (parseint (index) +1,this._ Cookie[name].length));
this._cookie[name]["Timeout"]=_ttime;
}
This. Getcount=function (name) {
return this._cookie[name].length;
}
This. Getitem=function (Name,index) {
return This._cookie[name][index];
}
}
</script>
<script language= "JScript" >
var cookieclass=new Cookie ();
if (! Cookieclass.load ()) {
Cookieclass.create ("Pass", 1);
Cookieclass.additem ("Pass", "Ps1");
Cookieclass.additem ("Pass", "PS2");
Cookieclass.additem ("Pass", "PS3");
Cookieclass.additem ("Pass", "PS4");
Cookieclass.delitem ("Pass", 1);
Cookieclass.save ();
}
Alert ("Cookie Expiration Time:" +cookieclass.gettime ("Pass"). toLocaleString ());
alert (Document.cookie);
</script>
<script>
function Eyuncookie ()
{this.key= "";//Initialize key.
This.value= "";//Initialize key ' s value.
this.expires=0;//the valid time, in milliseconds, to initialize the cookie.
This.init=function ()//object initialization
{this.key= "";
This.value= "";
this.expires=0;
}
This.set=function (key,value,expires)/Set Cookies
{if (this.key== "") This.key=key;
if (this.value== "") This.value=value;
if (this.expires<=0) this.expires=expires;
if (this.key== "" | | typeof (This.key)!= "string")
{Alert ("Please set the cookie name you want to save first!") ");
This.init ();
return false;
}
if (This.key.match (/[,;] /))
{Alert ("", ","; or a space! ");
This.init ();
return false;
}
if (This.value.toString (). Match (/[,;] /)|| typeof (This.value) = = "undefined")
{Alert ("", ",", ";" or a space! ");
This.init ();
return false;
}
if (this.expires<=0| | typeof (This.expires)!= "number")
{Alert ("Please set the valid time for cookies first!") ");
This.init ();
return false;
}
var Cookie=document.cookie;
if (Cookie.indexof (this.key+ "=")!=-1)
{if (!confirm) the cookie name you wish to save already exists, do you want to replace it? "))
{This.init ();
return false;
}
}
var dt=new Date ();
Dt.settime (Dt.gettime () +this.expires);
document.cookie=this.key+ "=" +this.value+ "; expires=" +dt.togmtstring ();
This.init ();
return true;
}
This.get=function (key)//Gets the value of the cookie named key
{if (key== "") | | Key.match (/[,;] /))
{Alert ("Please set the cookie name you want to find correctly!") ")
return false;
}
var Cookie=document.cookie;
var start=cookie.indexof (key+ "=");
if (start==-1)
{alert ("The cookie you want to find does not exist!") ")
return false;
}
var end=cookie.indexof (";", start);
if (end==-1)
End=cookie.length;
var getcookie=cookie.substring (start+key.length+1,end);
Alert ("Cookie: +key+" value is "+getcookie");
return GetCookie;
}
This.showall=function () {alert ("total of the following cookies: \ n" +document.cookie.split (";"). ToString (). Replace (/,/g, "\ n")); Show All Cookies
This.del=function (key)//delete cookie named key
{if (key== "") | | Key.match (/[,;] /))
{Alert ("Please set the cookie name you want to remove correctly!") ")
return false;
}
var dt=new Date ();
Dt.settime (Dt.gettime ());
document.cookie=key+ "=eyundelete;expires=" +dt.togmtstring ();
This.init ();
return true;
}
This.destroy=function ()//Destroy All Cookies
{var dt=new Date ();
Dt.settime (Dt.gettime ());
while (document.cookie!= "")
Document.cookie=document.cookie+ "; expires=" +dt.togmtstring ();
This.init ();
return True
}
}
var cookietest=new eyuncookie ()
function Settest ()
{cookietest.key= "test"
Cookietest.value= "OK"
cookietest.expires=31536000000
Cookietest.set ()
}
</script>
<input Type=button onclick=cookietest.showall () value=read><input Type=button value=setA><input type= Button Value=settest><input Type=button 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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.