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