Own js tool Cookie Encapsulation

Source: Internet
Author: User

In this case, we 'd better encapsulate cookie operations for reuse.
==================================
Copy codeThe Code is as follows:
/**
Cookie-like
Put this class into the js file for use.
1. add (name, value, 100); add a cookie
2. get (name );
3. remove (name );
Use Case:
Cookie. add ("sk", "ss", 3 );
Alert (cookie. get ("sk "));
Cookie. remove ("sk ");
*/
Var Cookie = new function (){
// Add cookie
This. add = function (name, value, hours ){
Var life = new Date (). getTime ();
Life + = hours * 1000*60;
Var cookieStr = name + "=" + escape (value) + "; expires =" + new Date (life). toGMTString ();
Document. cookie = cookieStr;
};
// Obtain the cookie value
This. get = function (name ){
Var cookies = document. cookie. split (";");
If (cookies. length> 0 ){
Var cookie = cookies [0]. split ("= ");
If (cookie [0] = name)
Return unescape (cookie [1]);
}
Return null;
};
// Delete the cookie
This. remove = function (name ){
Var cookieStr = name + "=" + escape ('null') + "; expires =" + new Date (). toGMTString ();
Document. cookie = cookieStr;
};
}

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.