This article mainly introduces the Cookie operation examples in JavaScript. This article first explains the Cookie syntax and then provides the actual operation examples. For more information, see
JavaScript Cookie
Cookie object:
Cookie is the user data (Cookie data) stored in the Cookies folder on the client's hard disk in the form of files ).
The Cookie file is created by the Accessed Web Site to Store session data between the client and the Web site for a long time. the Cookie data can only be read by the Accessed Web site.
Cookie file format:
NS: Cookie.txt
IE: user name @ domain name .txt
There are two types of cookies:
(1) persistent cookies are stored on the client's hard disk.
(2) session Cookie: it is stored in the memory of the browser process instead of the client's hard disk. when the browser is closed, the session cookie is destroyed.
Use JS to implement Cookie operations
Write Cookie:
The code is as follows:
Document. cookie = "keyword = value [; expires = Valid date] [;...]"
Read Cookie:
The code is as follows:
Document. cookie
Delete Cookie:
The code is as follows:
Document. cookie = "keyword =; expires = current date"
Note:
1. Valid date format: Wdy, DD-Mon-yy hh: MM: SS GMT
2. Wdy/Mon: English week/month;
3. it also contains the path, domain, and secure attributes;
4. each Web site (domain) can establish 20 Cookie data;
5. each browser can store 300 Cookie data, 4 K bytes;
6. the customer has the right to prohibit writing Cookie data.
Instance
The code is as follows:
CookieTest.html