Tool function encapsulation of JavaScript cookies

Source: Internet
Author: User
Tags set cookie

I. Syntax

1.1 obtain all cookies on the current page:

Var allCookies = document. cookie;
AllCookies are a string that contains the cookie list strings separated by semicolons (key = value ).

1.2 write a new cookie:

Document. cookie = updatedCookie;
UpdatedCookie is a string in the form of a key-value pair. You can only use this method to set or update a cookie at a time, and add a cookie instead of overwriting. For example:

Document. cookie = "fontSize = 14 ";
Document. cookie = "fontSize = 16 ";
Document. cookie = "fontColor = black ";

Document. cookie; // fontSize = 16; fontColor = black
1.3 optional attributes:

In addition to the Cookie content, there are also some optional attributes that can be written. Define cookie settings/updates and separate them with a semicolon:

Set-Cookie: value [; expires = date] [; domain = domain] [; path = path] [; secure]
(1) If path = path (for example, '/', '/mydir') is not defined, it is the path of the current document by default.
(2) domain = domain (for example, 'example. com ',' .example.com '(including all sub-domain names), and 'subdomain .example.com') if not defined, it is the domain name part of the path at the current file location by default.
(3) max-age = max-age-in-seconds (for example, 606024*365 a year)
(4) expires = date-in-GMTString-format if not defined, the cookie will expire at the end of the conversation. For the format of this value, see Date. toUTCString ().
(5) secure (cookie is transmitted only through https protocol) the value string of cookie can use encodeURIComponent () to ensure that it does not contain any comma, semicolon, or space (these values are not allowed in cookie values ).
II. cookie interface encapsulation:

Var cookieUtil = {
// Set cookie
SetItem: function (name, value, days ){
Var date = new Date ();
Date. setDate (date. getDate () + days );
Document. cookie = name + '=' + value + '; expires =' + date;
},

// Obtain the cookie
GetItem: function (name ){
Var arr = document. cookie. replace (/\ s/g, ""). split (';');
For (var I = 0; I <arr. length; I ++ ){
Var tempArr = arr [I]. split ('= ');
If (tempArr [0] = name ){
Return decodeURIComponent (tempArr [1]);
             }
         }
Return '';
},

// Delete the cookie
RemoveItem: function (name ){
This. setItem (name, '1',-1 );
},

// Check whether a cookie exists
HasItem: function (name ){
Return (new RegExp ("(? : ^ |; \ S *) "+ encodeURIComponent (name ). replace (/[\-\. \ + \ *]/g, "\ $ &") + "\ s * \ = ")). test (document. cookie );
},

// Obtain the list of all cookies
GetAllItems: function (){
Var cookieArr = document. cookie. replace (/((? : ^ | \ S *;) [^ \ =] + )(? =; | $) | ^ \ S * | \ s *(? : \ = [^;] *)? (? : \ 1 | $)/g, ""). split (/\ s *(? : \ = [^;] *)?; \ S */);
For (var nIdx = 0; nIdx <cookieArr. length; nIdx ++) {cookieArr [nIdx] = decodeURIComponent (cookieArr [nIdx]);}
Return cookieArr;
    }
};

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.