Js operation cookies include: Writing cookies, reading cookies, deleting cookies, and using examples. If you are interested, refer
The Code is as follows:
// JS cookies operation method!
// Write cookies
Function setCookie (name, value)
{
Var Days = 30;
Var exp = new Date ();
Exp. setTime (exp. getTime () + Days x 24x60*60*1000 );
Document. cookie = name + "=" + escape (value) + "; expires =" + exp. toGMTString ();
}
// Read cookies
Function getCookie (name)
{
Var arr, reg = new RegExp ("(^ |)" + name + "= ([^;] *) (; | $ )");
If (arr = document. cookie. match (reg) return unescape (arr [2]);
Else return null;
}
// Delete cookies
Function delCookie (name)
{
Var exp = new Date ();
Exp. setTime (exp. getTime ()-1 );
Var cval = getCookie (name );
If (cval! = Null) document. cookie = name + "=" + cval + "; expires =" + exp. toGMTString ();
}
// Example
SetCookie ("name", "hayden ");
Alert (getCookie ("name "));
// If you need to set the custom expiration time
// Replace the preceding setCookie function with the following two functions;
// Program code
Function setCookie2 (name, value, time ){
Var strsec = getsec (time );
Var exp = new Date ();
Exp. setTime (exp. getTime () + strsec * 1 );
Document. cookie = name + "=" + escape (value) + "; expires =" + exp. toGMTString ();
}
Function getsec (str ){
Alert (str );
Var str1 = str. substring (1, str. length) * 1;
Var str2 = str. substring (0, 1 );
If (str2 = "s "){
Return str1 * 1000;
} Else if (str2 = "h "){
Return str1 * 60x60*1000;
} Else if (str2 = "d "){
Return str1 * 24x60*60*1000;
}
}
// This is an example of setting the expiration time:
// S20 indicates 20 seconds
// H indicates the hour. For example, 12 hours indicates h12.
// D is the number of days, and 30 days is the number of days: d30
// Only the three types are currently written.
SetCookie2 ("name2", "hayden2", "s20 ");
Alert (getCookie ("name2 "));
The following are some common useful functions:
The Code is as follows:
Function GetCookieVal (offset)
// Obtain the decoded Cookie value
{
Var endstr = document. cookie. indexOf (";", offset );
If (endstr =-1)
Endstr = document. cookie. length;
Return unescape (document. 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 ));
Document. 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 );
Document. cookie = name + "=" + cval + "; expires =" + exp. toGMTString ();
}
Function GetCookie (name)
// Obtain the original Cookie value
{
Var arg = name + "= ";
Var alen = arg. length;
Var clen = document. cookie. length;
Var I = 0;
While (I <clen)
{
Var j = I + alen;
If (document. cookie. substring (I, j) = arg)
Return GetCookieVal (j );
I = document. cookie. indexOf ("", I) + 1;
If (I = 0) break;
}
Return null;
}
// Test
SetCookie ("sunshine", "1986 ");
Alert (GetCookie ("sunshine "));