JavaScript cookie details

Source: Internet
Author: User
I. cookie Overview
Cookie is an attribute of the Document Object.
It enables JavaScript code to persistently store data on users' hard disks and read, modify, and delete the data using JavaScript code.
In this way, information can be saved, and the information can still be operated upon when the current dialog is disabled and enabled again later. (For example, if you enter your email address in the text box, the text box will automatically display the email address you entered next time you open this page ).
Cookie has the following four optional attributes:
1. expires (used to specify the cookie retention period, which has been replaced by the max-age attribute)
2. path (used to specify which pages on the website can share cookie data)
3. domain (used to specify cross-Server Page sharing cookie data. For example, cookie data can be used for pages in catalog.example.com and orders. example)
4. secure (specifies how to transmit the cookie value on the network. Its value is a Boolean value. If the value is true, the transmission method is a secure HTTPS method, when the value is false, the transmission method is normal and insecure)

Ii. Storage, reading, modification, and deletion of cookies
Storage:

1. document. cookie = "name = vale"; (name is the cookie name, vale is the cookie value, and multiple cookies can be stored for multiple times)
2. document. cookie = "name =" + valiable; (the value can be a variable)
3. document. cookie = "name =" + encodeURIComponent (valiable); (encrypt the value and decrypt it when reading it. It will be discussed later)
4. document. cookie = "name =" + valiable + "; max-age =" + 1000; (use the max-age attribute to set the cookie retention time, represented by a number, 1 represents 1 second, you can also use mathematical methods such as; document. cookie = "name =" + valiable + "; max-age =" + (60*60*24*356); indicates a year)
4. document. cookie = "name =" + valiable + "; max-age =" + 1000 + "; path =/; domain = .baidu.com; secure = true "; (The path is "/", which indicates that the page is under the root directory, for example, www.baidu.com, if the domain is ".baidu.com", it indicates that all the pages of the server under this domain, such as those under "search.baidu.com" and "hi.baidu.com", are allowed, if secure is set to true, secure HTTPS transmission is used .)

Read:
Use document. cookie to read the cookie. It returns a string that stores all cookies applied to the current document. It is a list of name = value pairs, separated by semicolons, excluding the cookie attributes that have been set. You can use alert (document. cookie); to view these values.
As stated in the storage, encodeURIComponent (valiable) is used for encoding. You can use the decodeURIComponent () method to decode the data during reading. For example, alert (encodeURIComponent (document. cookie ));

Modify:
The so-called modification is actually to set different values with the same name.
For example:
Document. cookie = "name = vale ";
Document. cookie = "name = vale2 ";
In this way, the cookie value is changed. It turns out that vale is now vale2. Of course, changing the expiration time is also resetting.

Delete:
Delete is to set the value to null.
For example:
Document. cookie = "name = vale ";
Document. cookie = "name = ";

Note:
The cookie value cannot contain semicolons (;), commas (,), and blank characters.
The first three methods can only survive in the current browser session, and will be lost when the user exits the browser.
The four attributes of a cookie are optional. You can either write one or more or not.

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.