JavaScript Cookies
Cookie Object:
Cookies are user data information (Cookie data) stored in the cookies folder of the client's hard drive in the form of a file.
The cookie file is created by the visited Web site to save the session data between the client and the Web site for a long time, and the cookie data is only allowed to be read by the visited Web site.
Cookie file format:
NS: Cookie.txt
There are two types of cookies:
(1) Persistent cookies will be stored on the client's hard disk.
(2) Session cookies: They are not stored on the client's hard disk, but are placed in the memory of the browser process. When the browser is closed, the session cookies will be destroyed.
Implement cookie operations with JS
Write cookies:
document.cookie = "Keyword = Value [ ; expires = Valid date] [;...]"
Read Cookies:
document.cookie
Delete Cookies:
document.cookie = "Keyword = ; expires = current date"
Remark:
1. Valid date format: Wdy, DD-Mon-YY HH:MM:SS GMT
2.Wdy / Mon: English week / month;
3. It also contains path, domain, and secure attributes;
4. Each Web site (domain) can create 20 cookie data;
5. Each browser can store 300 cookie data and 4k bytes;
6. Customers have the right to prohibit the writing of cookie data.
Example
The code copy is as follows:
<!DOCTYPE html>
<html>
<head>
<title>cookieTest.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<script type="text/javascript">
var today = new Date();
var expiredDay = new Date();
var msPerMonth = 1000 * 60 * 60 * 24 * 30;
expiredDay.setTime(today.getTime() + msPerMonth); //Expiration in one month
//Write cookies
document.cookie = "name=mengdd;expires="+expiredDay.toGMTString();
document.writeln("cookie has been written to the hard drive");
//Read cookies
document.writeln("Content is:" + document.cookie);
document.writeln("expire day: " + expiredDay.toGMTString());
</script>
</body>
</html>
The above is the entire content of the cookie object in JavaScript. I hope everyone likes it.