Js Code for Cookie operations, with detailed comments in the code

Source: Internet
Author: User

Js operation Cookie Code. The Code below contains detailed comments, so I will not talk about it here. go directly to the Code:

/*
* Set and retrieve cookies
*/
Var Cookie = {}
// Write the Cookie. The key is the key and the value is the value.
// Duration expiration time (in days, the default value is 1 day)
Cookie. write = function (key, value, duration)
{
Cookie. remove (key );
Var d = new Date ();
If (duration <= 0)
Duration = 1;
D. setTime (d. getTime () + 1000*60*60*24 * duration );
Document. cookie = key + "=" + encodeURI (value) + "; expires =" + d. toGMTString () + "; path = /";
};
// Remove the Cookie. The key is the key.
Cookie. remove = function (key)
{
Var d = new Date ();
If (Cookie. read (key )! = "")
{
D. setTime (d. getTime ()-(86400*1000*1 ));
Document. cookie = key + "=; expires =" + d. toGMTString ();
}
};
// More Tutorial: Veryhuo. Com
// Read the Cookie. The key is the key.
// No empty string is returned ""
Cookie. read = function (key)
{
Var arr = document. cookie. match (new RegExp ("(^ |)" + key + "= ([^;] *) (; | $ )"));
If (arr! = Null)
Return decodeURIComponent (arr [2]);
Return "";
};

Related Article

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.