Cookie object for JavaScript learning notes, learning notes cookie
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:
Document. cookie = "keyword = value [; expires = valid date] [;...]"
Read Cookie:
Document. cookie
Delete 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. 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
Copy codeThe Code 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); // expire after one month
// Write cookie
Document. cookie = "name = mengdd; expires =" + expiredDay. toGMTString ();
Document. writeln ("the cookie has been written to the hard disk ");
// Read cookie
Document. writeln ("content:" + document. cookie );
Document. writeln ("expire day:" + expiredDay. toGMTString ());
</Script>
</Body>
</Html>
The above is all the content of the cookie object in javascript. I hope you will like it.