Javascriptcookies are also a good article to store, retrieve, and delete instances. I have just compiled some articles about javascript cookies. I found this article is also good. I suggest you refer to it and select what you need. The main disadvantage is setting the path, you can combine your favorite friends.
The Code is as follows:
Script
Function SetCookie (name, value) // two parameters: one is the name of the cookie and the other is the value.
{
Var Days = 30; // This cookie will be saved for 30 Days
Var exp = new Date (); // new Date ("December 31,999 8 ");
Exp. setTime (exp. getTime () + Days x 24x60*60*1000 );
Document. cookie = name + "=" + escape (value) + "; expires =" + exp. toGMTString ();
}
Function getCookie (name) // The cookie function.
{
Var arr = document. cookie. match (new RegExp ("(^ |)" + name + "= ([^;] *) (; | $ )"));
If (arr! = Null) return unescape (arr [2]); return null;
}
Function delCookie (name) // delete a cookie
{
Var exp = new Date ();
Exp. setTime (exp. getTime ()-1 );
Var cval = getCookie (name );
If (cval! = Null) document. cookie = name + "=" + cval + "; expires =" + exp. toGMTString ();
}
SetCookie ("xiaoqi", "3 ")
Alert (getCookie ('xiaoqi '));
Script
A very practical javascript function for reading and writing cookies
The Code is as follows:
Function GetCookieVal (offset)
// Obtain the decoded Cookie value
{
Var endstr = documents. cookie. indexOf (";", offset );
If (endstr =-1)
Endstr = documents. cookie. length;
Return unescape (events. cookie. substring (offset, endstr ));
}
Function SetCookie (name, value)
// Set the Cookie value
{
Var expdate = new Date ();
Var argv = SetCookie. arguments;
Var argc = SetCookie. arguments. length;
Var expires = (argc> 2 )? Argv [2]: null;
Var path = (argc> 3 )? Argv [3]: null;
Var domain = (argc> 4 )? Argv [4]: null;
Var secure = (argc> 5 )? Argv [5]: false;
If (expires! = Null) expdate. setTime (expdate. getTime () + (expires * 1000 ));
Events. cookie = name + "=" + escape (value) + (expires = null )? "": ("; Expires =" + expdate. toGMTString ()))
+ (Path = null )? "": ("; Path =" + path) + (domain = null )? "": ("; Domain =" + domain ))
+ (Secure = true )? "; Secure ":"");
}
Function DelCookie (name)
// Delete the Cookie
{
Var exp = new Date ();
Exp. setTime (exp. getTime ()-1 );
Var cval = GetCookie (name );
Documents. cookie = name + "=" + cval + "; expires =" + exp. toGMTString ();
}
Function GetCookie (name)
// Obtain the original Cookie value
{
Var arg = name + "= ";
Var alen = arg. length;
Var clen = documents. cookie. length;
Var I = 0;
While (I <clen)
{
Var j = I + alen;
If (documents. cookie. substring (I, j) = arg)
Return GetCookieVal (j );
I = documents. cookie. indexOf ("", I) + 1;
If (I = 0) break;
}
Return null;
}
The Code is as follows: