Javascript-cookie string description

Source: Internet
Author: User

Javascript-cookie string description

A complete cookie requires six substrings: name (name), value (value), expires (expiration time), path (path), domain (domain), and secure (Security ), the last four substrings are optional. the following describes the settings of these six substrings one by one.

 

1. nameAndValue

Name is used to reference the cookie, and value is the information saved by the cookie. name/value pair must be set. For example:

 

 document.cookie = 'UserName=Paul';

 

The cookie value is a simple string. If you want to save data of the numerical type, it must also be saved in the cookie format. unless these special characters are specially encoded, some characters such as semicolons (;) cannot be contained in the value string when the string is saved in the cookie, this is because the semicolon is used as the separator for different parts in the cookie string.

 

2. expires (Expiration time)

A cookie only has a limited life cycle. If the expiration time of a cookie is not set, the cookie will expire when the user closes the browser. when the user opens the browser again, the cookie no longer exists. this cookie is called a session cookie. user session refers to the duration of a user's Website access. if a cookie is used for a longer period of time, you must set an expiration time for the cookie, as shown below:

 

var date=new Date();date.setDate((date.getDate()+7));document.cookie ='UserName='+'P'+';expires='+date;

 

The previous Code sets the cookie expiration time to 7 days after the current time (cookie creation time. other factors may cause the cookie to expire before the expiration time. For example, when the user manually deletes the cookie, or the number of cookies reaches the maximum number of cookies supported by the browser.

(Note:The expiration time format is important, especially for IE browsers. the cookie expiration time must be in the same format as the date and time returned by the toGMTString () method. in actual use, we usually use the above method, first use the Date object to get the time, and then assign the expiration time)

 

3. path (Path)

The web server rationally divides the entire website into different directories, rather than putting all the files of the entire website in one directory. What is the relationship between this and cookie? Cookie is not only specific to a domain, but also specific to the specific path in the domain. For example:

 

Var path = '/E:/JS/demo'; document. cookie = 'user =' + encodeURIComponent ('xu fengzhu ') +'; path = '+ path;

 

But what if we want to access a cookie from two different paths? It's easy to set the cookie path as the parent directory path. Based on the above logic, we can implement it.

It is worth noting that although the directory names in windows are not case sensitive, other operating systems may be case sensitive to the directory names. for example, if a website runs on a Unix or Linux-based server, the path tag is case sensitive.

 

4. domain (Domain)

By default, the cookie is only valid for the domain of the page on which the cookie is set. for example, two sites are running on a web server. One is the site with the domain name a.book.com and the other is the site with the domain name B .book.com. the cookie is set on one site, and the other is inaccessible. in most cases, this is exactly what we want, but in special cases it is different. at this time, we can use the domain sub-string to set a domain name so that the domain name and all its subdomains can access the cookie. the following code creates a cookie. The book.com domain and all its subdomains can access the cookie.

 

Document. cookie = 'user = '+ encodeURIComponent ('xu fengzhu') + '; domain = book.com ';

 

Note:Only the book.com domain and all its subdomains can access this cookie. Other domains cannot.

 

5. secure (Security)

Secure is a Boolean value that specifies how the cookie value is transmitted over the network. By default, the cookie will ignore Security

Problem, always transmitted through a common http connection. however, if you set the cookie security flag to true, the cookie will be transmitted only when the browser and server are connected through the SSL (Secure Sockets Layer, Secure Socket Layer) security protocol.

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.