Cookie manipulation instances in JavaScript _javascript tips

Source: Internet
Author: User

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:

Copy Code code as follows:

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

Read cookies:
Copy Code code as follows:

Document.cookie

To delete a cookie:
Copy Code code as follows:

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>


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.