JavaScript Action Cookie

Source: Internet
Author: User


The composition of the cookie:
A cookie consists of the following parts:
1. Name
2. Value
3. Other attributes (max-age, path, domain, secure)

    • Max-age:cookie the presence of a second tree, max-age=1000, that is, this cookie will be deleted after 1000 seconds.
    • Path and domain: The domain name and path define the visible range of a cookie, which tells the browser where the cookie belongs. For security reasons, domain can only be set to the upper domain name of the current domain name. For example, you cannot set the cookie domain to a.example.com under example.com, and a.example.com can set a cookie for the domain example.com, for path, if a cookie Path is/doc, so you can access this cookie on all pages below/doc/(if domain is also a requirement)
    • After secure sets this property, the cookie becomes a secure cookie that is only carried when the encryption is transmitted.

1. Setting cookies

just assign a value directly above the Document.cookie, it will not affect the original cookie value ```

functionSetcookie (name, value, daytolive, option) {varCookie = name + ' = ' +encodeURI (value); if(typeofdattolive = = = ' Number ') {Cookie+ = '; max-age= ' + daytolive * 24 * 60 * 60; }  if(option) { for(varKeyinchoption) {    if(Key = = ' domain ') {Cookie+ = ';d omain= ' +Option[key]; } Else if(Key = = ' path ') {Cookie+ = ';p ath= ' +Option[key]; } Else{Cookie+ = '; Secure '; } }} document.cookie=cookies;}

2. Obtaining cookies
Browser can not get the cookie expiration time, domain, path and other information, multiple cookies are separated by (; space)/;\s/

function GetCookie (name) {var cookie = document.cookie; var cookiearr = cookie.split (/;\s/);  for (var i = 0; i < cookiearr.length; i++) {var pair = cookiearr[i].split (' = '); if (Pair[0] = = name) {return decodeURI (pair[1]);}} return NULL ;}

JavaScript Action Cookie

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.