Home >Web Front-end >JS Tutorial >Cookie operation examples in JavaScript_javascript skills

Cookie operation examples in JavaScript_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:21:281197browse

JavaScript Cookie

Cookie object:
Cookie is a kind of user data information (Cookie data) saved in the Cookies folder of the client's hard disk in the form of a file.
The cookie file is created by the visited Web site to permanently save the session data between the client and the Web site, and the cookie data is only allowed to be read by the visited Web site.
Cookie file format:
NS: Cookie.txt
IE: username@domain name.txt


There are two types of cookies:
(1) Persistent cookies will be stored on the client's hard drive.
(2) Session Cookie: It will not be stored on the client's hard drive, but will be placed in the memory of the browser process. When the browser is closed, the session cookie will be destroyed.

Use JS to implement Cookie operations

Write Cookie:

Copy code The code is as follows:

​document.cookie = "keyword = value [ ; expires = valid date ] [;...]"

Read Cookie:
Copy code The code is as follows:

document.cookie

​Delete Cookies:
Copy code The code is as follows:

 document.cookie = "keyword = ; expires = current date"

Remarks:

 1. Valid date format: Wdy,DD-Mon-YY HH:MM:SS GMT
​2.Wdy/Mon: English week/month;
3. Also includes path, domain, and secure attributes;
4. Each website (domain) can create 20 cookie data;
5. Each browser can store 300 cookie data, 4k bytes;
6. Customers have the right to prohibit the writing of Cookie data.

Example

Copy code The code is as follows:




cookieTest.html
 



 








Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn