Use JS to read, write, delete cookies code sharing and detailed notes _javascript tips

Source: Internet
Author: User
Tags setcookie delete cache

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 ";
}
}

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.