Js operation Cookie Code. The Code below contains detailed comments, so I will not talk about it here. go directly to the Code:
/*
* Set and retrieve cookies
*/
Var Cookie = {}
// Write the Cookie. The key is the key and the value is the value.
// Duration expiration time (in days, the default value is 1 day)
Cookie. write = function (key, value, duration)
{
Cookie. remove (key );
Var d = new Date ();
If (duration <= 0)
Duration = 1;
D. setTime (d. getTime () + 1000*60*60*24 * duration );
Document. cookie = key + "=" + encodeURI (value) + "; expires =" + d. toGMTString () + "; path = /";
};
// Remove the Cookie. The key is the key.
Cookie. remove = function (key)
{
Var d = new Date ();
If (Cookie. read (key )! = "")
{
D. setTime (d. getTime ()-(86400*1000*1 ));
Document. cookie = key + "=; expires =" + d. toGMTString ();
}
};
// More Tutorial: Veryhuo. Com
// Read the Cookie. The key is the key.
// No empty string is returned ""
Cookie. read = function (key)
{
Var arr = document. cookie. match (new RegExp ("(^ |)" + key + "= ([^;] *) (; | $ )"));
If (arr! = Null)
Return decodeURIComponent (arr [2]);
Return "";
};