Javascript Summary-cookie
Cookies in javascript can be used to save multiple pieces of information, and each item is stored in the following format: Information name = Information Content
The cookie contains some control fields, such as expires, which indicates the cookie expiration time.
Today, I have summarized the cookie operations. All the source code is as follows. I will not elaborate on them one by one when I get off work.
/**//*********************************** ******
Obtain the cookie value based on the name.
**************************************** ***/
Function getcookie (name )...{
VaR value = Document. Cookie;
VaR arr1 = value. Split (";");
For (I = 0; I <arr1.length; I ++ )...{
If (value. Length = 0 )...{
Break;
}
Sname = arr1 [I]. Split ("=") [0];
If (sname = Name )...{
Return arr1 [I]. Split ("=") [1];
}
}
Return NULL;
}
/**//*********************************** ******
Set cookie content
**************************************** ***/
Function setcookie (sname, svalue)... {// create a cookie
// It's a bit similar to the anonymous class.
VaR expires = function ()... {// cookie expiration time is within 48 hours
VaR mydate = new date ();
Mydate. settime (mydate. gettime + 48*60x60*1000 );
Return mydate. togmtstring ();
}
If (sname. length! = 0 & svalue. length! = 0 )...{
Document. Cookie = sname + "=" + svalue + "; expires =" + expires;
} Else ...{
Alert ("You are blank! ");
}
}
/**//*********************************** ******
Delete cookie
**************************************** ***/
Function delcookie (sname, svalue)... {// delete a specified cookie key-Value Pair
Document. Cookie = sname + "=" + escape (svalue) + "; expires = Fri, 31 Dec 1999 23:59:59 GMT ;";
}
/**//*********************************** ******
Delete cookie
**************************************** ***/
Function delcookie (sname )...{
VaR svalue = getcookie (sname );
If (svalue! = NULL )...{
Document. Cookie = sname + "=" + escape (svalue) + "; expires = Fri, 31 Dec 1999 23:59:59 GMT ;";
}
}
/**//*********************************** ******
Clear cookie
**************************************** ***/
Function clearcookie ()...{
VaR value = Document. Cookie;
VaR arr1 = value. Split (";");
For (I = arr1.length-1; I> = 0; I --)...{
If (value. Length = 0 )...{
Break;
}
Sname = arr1 [I]. Split ("=") [0];
Svalue = arr1 [I]. Split ("=") [1]
Document. Cookie = sname + "=" + escape (svalue) + "; expires = Fri, 31 Dec 1999 23:59:59 GMT ;";
}
}