// Verified
// JavaScript Document
// Instructions for use:
// Set cache: setCookie ("name", value );
// Obtain the cache: var name = getCookie ("name ");
// Delete cache: delCookie ("name ");
/// Set cookie
Function setCookie (NameOfCookie, value, expiredays)
{
// @ Parameter: three variables are used to set the new cookie:
// Cookie name, stored Cookie value,
// And the Cookie expiration time.
// These rows convert the number of days to a valid date.
Var ExpireDate = new Date ();
ExpireDate. setTime (ExpireDate. getTime () + (expiredays * 24*3600*1000 ));
// The following line is used to store cookies. You only need to assign values to "document. cookie.
// Note that the date is converted to the GMT time by using the toGMTstring () function.
Document. cookie = NameOfCookie + "=" + escape (value) + (expiredays = null )? "": "; Expires =" + ExpireDate. toGMTString ());
}
/// Obtain the cookie value
Function getCookie (NameOfCookie)
{
// First, check whether the cookie exists.
// If the cookie does not exist, the document. cookie length is 0.
If (document. cookie. length> 0)
{
// Check whether the cookie name exists in document. cookie.
// Because more than one cookie value is stored, even if the length of document. cookie is not 0, the cookie with the name we want cannot exist.
// So we need this step to check whether the cookie we want exists.
// If the begin variable is worth-1, it indicates that it does not exist.
Begin = document. cookie. indexOf (NameOfCookie + "= ");
If (begin! =-1)
{
// Indicates that our cookie exists.
Begin + = NameOfCookie. length + 1; // the initial position of the cookie value
End = document. cookie. indexOf (";", begin); // end position
If (end =-1) end = document. cookie. length; // No; end indicates the end position of the string.
Return unescape (document. cookie. substring (begin, end ));
}
}
Return null;
// If the cookie does not exist, null is returned.
}
/// Delete a cookie
Function delCookie (NameOfCookie)
{
// Check whether the cookie is set. If so, adjust the expiration time to the past time;
// Leave the cookie to the operating system for proper time.
If (getCookie (NameOfCookie ))
{
Document. cookie = NameOfCookie + "=" + "; expires = Thu, 01-Jan-70 00:00:01 GMT ";
}
}
From the online world