Home Web Front-end JS Tutorial JavaScript Learning Notes Cookie Object_Basic Knowledge

JavaScript Learning Notes Cookie Object_Basic Knowledge

May 16, 2016 pm 04:18 PM
cookie object javascript

JavaScript Cookie

Cookie object:

Cookie is a kind of user data information (Cookie data) saved in the Cookies folder of the client's hard disk in the form of a file.

The cookie file is created by the visited Web site to permanently save the session data between the client and the Web site, and the cookie data is only allowed to be read by the visited Web site.

Cookie file format:

NS: Cookie.txt

IE: username@domain name.txt

There are two types of cookies:

 (1) Persistent cookies will be stored on the client’s hard drive.

(2) Session Cookie: It will not be stored on the client's hard drive, but will be placed in the memory of the browser process. When the browser is closed, the session cookie will be destroyed.

Use JS to implement Cookie operations

Write Cookie:

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

Read Cookie:

Document.cookie

Delete cookies:

 document.cookie = "keyword = ; expires = current date"

Remarks:

 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 website (domain) can create 20 cookie data;

5. Each browser can store 300 cookie data, 4k bytes;

6. Customers have the right to prohibit the writing of cookie data.

Example

Copy code The 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); //Expires 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("The 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 you will like it.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to implement an online speech recognition system using WebSocket and JavaScript

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to implement an online reservation system using WebSocket and JavaScript

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

How to use JavaScript and WebSocket to implement a real-time online ordering system

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecasting system

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

Simple JavaScript Tutorial: How to Get HTTP Status Code

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

How to use insertBefore in javascript

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

How to get HTTP status code in JavaScript the easy way

See all articles