Basic JavaScript Learning (15)

Source: Internet
Author: User

JavaScript cookies

Learning Essentials:

Cookie Introduction

Create and get cookies

Encoding of Cookies

The lifetime of a cookie

Path of the cookie

Domain of Cookies

Secure the cookie

I. Introduction of Cookies

A cookie is actually some information that is stored on the client computer as a file. In JavaScript, cookies are used primarily to preserve state or to identify identities.

Ii. creation and acquisition of cookies

The syntax code for creating a cookie is as follows: Document.cookie= "Name=value"

The syntax code for getting a cookie is as follows: Document.cookie

Third, the encoding of the cookie

Use the Escape () function to encode the cookie value and restore it by using the unescape () function when reading the cookie.

Iv. the lifetime of a cookie

By default, when the browser is closed, the cookie disappears. If you want to continue to use it after the browser window, you need to set a lifetime for the cookie.

To set the lifetime of a cookie, you can use the following code:

Document.cookie=name=value;expires=date;

Date format: Wdy,dd-mon-yy HH:MM:SS GMT

V. Path to cookies

By setting up path=/to achieve the purpose of accessing cookies throughout the site.

Vi. Domain of Cookies

Cookies can be used to resolve the issue of cookie access in different virtual directories, but path does not address the issue of accessing cookies in different domains.

such as: Www.aspxfans.com and booksupport.aspxfans.com, set the domain of the cookie to. aspxfans.com just fine.

Vii. Secure the cookie

By default, cookies use a normal, unencrypted HTTP transport method. After setting the secure cookie, the cookie can only be transmitted over HTTPS or under other security protocols.

Basic JavaScript Learning (15)

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.