Copy Code code as follows:
Already verified.
JavaScript Document
Instructions for use:
Set cache: Setcookie ("name", value);
Get cache: var Name=getcookie ("name");
Delete cache: Delcookie ("name");
Set cookies
function Setcookie (Nameofcookie, value, expiredays)
{
@ parameter: Three variables are used to set the new cookie:
The name of the cookie, the stored cookie value,
And when the cookie expires.
These lines are converting the days to valid dates
var expiredate = new Date ();
Expiredate.settime (Expiredate.gettime () + (Expiredays * 24 * 3600 * 1000));
The following line is used to store cookies, simply by assigning a value to "Document.cookie".
Note that the date is converted to GMT time through the togmtstring () function.
Document.cookie = Nameofcookie + "=" + Escape (value) + ((expiredays = null)? "" : "; Expires= "+ expiredate.togmtstring ());
}
Get Cookie Value
function GetCookie (Nameofcookie)
{
First we check to see if the cookie exists.
If it does not exist, the length of the Document.cookie is 0.
if (Document.cookie.length > 0)
{
And then we check to see if the cookie name exists in Document.cookie.
Because more than one cookie value is stored, even the document.cookie length of 0 does not guarantee that the cookie of the name we want exists
So we need this step to see if we have the cookie we want.
If the variable to begin is worth 1, then it doesn't exist.
Begin = Document.cookie.indexOf (nameofcookie+ "=");
if (begin!=-1)
{
Indicates the existence of our cookies.
Initial position of Begin = = Nameofcookie.length+1;//cookie value
End = Document.cookie.indexOf (";", begin);/Ending position
if (end = = 1) ends = document.cookie.length;//not;
Return unescape (document.cookie.substring (begin, end));
}
}
return null;
Cookie does not exist return null
}
Delete Cookies
function Delcookie (Nameofcookie)
{
The function checks whether the cookie is set, and if it is set, sets the expiration time to the previous time;
The rest is left to the operating system to clean up cookies at the right time.
if (GetCookie (Nameofcookie))
{
Document.cookie = Nameofcookie + "=" + "; Expires=thu, 01-jan-70 00:00:01 GMT ";
}
}