tutorial on the use of cookies in JavaScript

Source: Internet
Author: User
Tags set cookie

This article mainly introduces the use of cookies in JavaScript tutorial, including cookies storage and deletion of the operation methods, the need for friends can refer to the

What is a cookie?

Web browsers and servers communicate using the HTTP protocol, and HTTP is a stateless protocol. But for a commercial website it needs to keep the conversation information between different pages. For example, after multiple pages are completed, a user registration ends. However, how to maintain session information for all Web page users.

In many cases, using cookies to memorize and track preferences, purchase, commission, and require better access to experience or Web site statistics and other information is the most effective method.

How does it work?

The server sends some data to the visitor's browser in the form of a cookie. The browser can accept cookies. If so, it is a plain-text record for the visitor to store on the hard drive. Now, when a visitor arrives at another page of your site, the browser sends the same cookie to the server for retrieval. Once retrieved, the server knows/remembers what just happened to the store.

Cookies have 5 plain text data records in variable-length fields:

Date the Expires:cookie will expire. If this is blank, then when the visitor exits the browser cookie will expire.

Domain: The name of the site.

Path: Paths Set cookie Directories or Web pages. If you want to get from any directory or page, then the cookie is empty.

Secure: If the field contains the word "safe", cookies can retrieve only a secure server. If the field is empty, there are no restrictions.

Name=value:cookie are set in the form of key and value pairs to obtain.

Cookies were originally designed for CGI programming and cookies are automatically transmitted between Web browsers and Web servers, so CGI scripts on the server can read and write the values of cookies stored on the client.

JavaScript can also manipulate the use of the Document object's cookie properties. JavaScript can read, create, modify, and delete cookies or cookies that apply to the current Web page.

Store Cookies:

The simplest way to create a cookie is to assign a string value to the Document.cookie object, which is:

Grammar

Document.cookie = "Key1=value1;key2=value2;expires=date";

Here, the Expires property option. If you provide this property with a valid date or time, the cookie will expire at a given date or time, and the value of the cookies will not be accessible.

Note: The value of the cookie may not include semicolons, commas, or whitespace. For this reason, you may want to use the JavaScript escape () function to encode the value that it stores before the cookie. If you do this, you must also use the appropriate unescape () function when reading the value of the cookie.

Read cookies:

Reading a cookie is as simple as writing because the value of Document.cookieobject is a cookie. So whenever you want to access cookies, you can use this string.

The string document.cookie will continue to be separated by a semicolon, where name is the name of a cookie, and the value is a list of name = value pairs delimited by its string value.

Set the expiration date for cookies:

You can extend the lifetime beyond the current browser session cookie by setting the expiration date and the expiration date in the Save cookie. This can be done by setting the date and time of the Expires property.

Example:

The following example shows how to set the expiration date after a cookie1 month:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22-23

To delete a cookie:

Sometimes if you want to delete a cookie so that subsequent attempts to read the cookie return what. To do this, you only need to set the expiration date at some time in the past.

Example:

The following example shows how to delete a cookie by setting a validity period of one months:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22-23

Note: You can use the SetTime () function to see the new value without setting the date.

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.