Cookie object for JavaScript learning notes, learning notes cookie

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.