JavaScript DOM-10 Cookies (cookie overview, cookie manipulation)

Source: Internet
Author: User

I. Overview of Cookies


Cookie Introduction

-Cookies are key/value pairs that are generated by the server and stored in the client file system (. txt format), which automatically sends the saved Cookie back to the server when the browser requests the page on that site again

-Cookies allow the browser to pass data between different requests that visit the same site, allowing the server program to identify different clients

-Typical application: Save the user login status, tracking user behavior, page customization, save the shopping cart, etc. need to save the global variables of the occasion

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7E/69/wKiom1b-L0ig-2v4AAB3IqQ4_Vs695.png "title=" Web.png "alt=" Wkiom1b-l0ig-2v4aab3iqq4_vs695.png "/>


Storage of cookies

-Cookies are stored in a. txt text file in a specific directory on the client, and cookie data from different sites are kept in different files

-Cookie data is usually saved after encryption

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7E/66/wKioL1b-MD_hH4ybAAGJmEcK8TY200.png "title=" Web.png "alt=" Wkiol1b-md_hh4ybaagjmeck8ty200.png "/>


Expiration and scope

-Validity period: A cookie can specify a expires value that defines its lifetime, during which the cookie is valid and the cookie is cleared. Some pages will have a cookie with a lifetime of "0" or a negative value, so that when the browser is closed, the cookie is cleared immediately, the user information is not recorded, and more secure


-Scope: By default, the cookie saved by a site can only be accessed by itself and cannot be used by other sites. However, you can set the domain and path values of the cookie, and define which paths under which domain can access the cookie


Second, Cookie operation


Save cookies

-Cookies can be created by server-side programs (Java, PHP, etc.) and sent to the client for saving, or can be created and saved by client JavaScript scripts

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7E/69/wKiom1b-MVjCHQeeAALSPbHzjY4039.png "title=" Web.png "alt=" Wkiom1b-mvjchqeeaalspbhzjy4039.png "/>


Read cookies

-Allows Document.cookie to obtain all cookies that the current site can read (between multiple cookies; separate)

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7E/6A/wKiom1b-McSBpJIsAAIXjhZfLcI405.png "title=" Web.png "alt=" Wkiom1b-mcsbpjisaaixjhzflci405.png "/>


The life cycle of a Cookie

-If the Expires attribute is not specified, the cookie created is actually saved in memory, and the browser is destroyed when it is closed.

-You can use expires to specify its life cycle when saving cookies

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7E/66/wKioL1b-M2Wwwj9RAAG1gUAuSB8081.png "title=" Web.png "alt=" Wkiol1b-m2wwwj9raag1guausb8081.png "/>



Summary: This chapter covers JavaScript DOM cookies (Cookie overview, cookie manipulation)


This article from the "Flying Ants" blog, declined to reprint!

JavaScript DOM-10 Cookies (cookie overview, cookie manipulation)

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.