Use js to read, write, and delete Cookie code sharing and detailed comments

Source: Internet
Author: User
Tags set cookie delete cache

Copy codeThe Code is as follows:
// 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 ";
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.