In this case, we 'd better encapsulate cookie operations for reuse.
==================================
Copy codeThe Code is as follows:
/**
Cookie-like
Put this class into the js file for use.
1. add (name, value, 100); add a cookie
2. get (name );
3. remove (name );
Use Case:
Cookie. add ("sk", "ss", 3 );
Alert (cookie. get ("sk "));
Cookie. remove ("sk ");
*/
Var Cookie = new function (){
// Add cookie
This. add = 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;
};
// Obtain the cookie value
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;
};
// Delete the cookie
This. remove = function (name ){
Var cookieStr = name + "=" + escape ('null') + "; expires =" + new Date (). toGMTString ();
Document. cookie = cookieStr;
};
}