I have released a javascript operation cookie. I have a lot of knowledge in it. You can search for it on this site. In addition, I have added two methods for js operation cookie. One is a simple version and the other is an encapsulation version, I would like to share with you a simple version of javascript For cookie operations.
The Code is as follows:
Function setCookie (name, value, iDay ){
Var oDate = new Date ();
ODate. setDate (oDate. getDate () + iDay );
Document. cookie = name + '=' + value + '; expires =' + oDate;
}
Function getCookie (name ){
Var arr = document. cookie. split (';');
Var I = 0;
For (I = 0; ivar arr2 = arr [I]. split ('= ');
If (arr2 [0] = name) {return arr2 [1];}
}
Return '';
}
Function removeCookie (name ){
SetCookie (name, '',-1 );
}
Cookie encapsulation version for javascript operations
The Code is as follows:
Var cookie = new function (){
This. set = function (name, value, hours ){
Var life = new Date (). getTime ();
Life + = hours * 1000*60;
Var cookieStr = name + "=" + escape (value) + "; expires =" + new Date (life). toGMTString ();
Document. cookie = cookieStr;
};
This. get = function (name ){
Var cookies = document. cookie. split (";");
If (cookies. length> 0 ){
Var cookie = cookies [0]. split ("= ");
If (cookie [0] = name) {return unescape (cookie [1]);}
}
Return null;
};
This. remove = function (name ){
Var cookieStr = name + "=" + escape ('null') + "; expires =" + new Date (). toGMTString ();
Document. cookie = cookieStr;
};
}
Keep the backup.