JavaScript Cookies
Cookie object:
A cookie is a user data information (cookie data) that is stored as a file in a cookie folder on the client's hard disk.
The cookie file is established by the Web site visited to hold 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 Web site visited.
Format of cookie file:
NS:Cookie.txt
IE: User name @ domain. txt
There are two types of cookies:
(1) Persistent cookies, which are stored on the client's hard disk.
(2) Session Cookie: It is not stored on the client's hard disk, but in the memory of the browser process, and the session cookie is destroyed when the browser closes.
Use JS to implement cookie operation
Write Cookie:
Document.cookie = "keyword = value [; expires = valid date] [; ...] "
Read cookies:
Document.cookie
To delete a cookie:
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. Also includes path, domain, secure attributes;
4. Each Web site (domain) can create 20 cookie data;
5. Each browser can store 300 cookie data, 4k bytes;
6. The customer has the right to prohibit the writing of cookie data.
Instance
Copy Code code as follows:
<! DOCTYPE html>
<title>cookieTest.html</title>
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >
<meta http-equiv= "description" content= "This are my page" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<!--<link rel= "stylesheet" type= "Text/css" href= "./styles.css" >-->
<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); Expires after one months
Write Cookie
Document.cookie = "name=mengdd;expires=" +expiredday.togmtstring ();
Document.writeln ("Cookies have been written to the hard disk");
Read cookies
Document.writeln ("content is:" + document.cookie);
Document.writeln ("Expire Day:" + expiredday.togmtstring ());
</script>
</body>
The above is the entire contents of the cookie object in JavaScript, I hope you can enjoy it.