How to use cookies through Javascript

Source: Internet
Author: User
First, let's briefly introduce the cookie. Simply put, the cookie is the information that the server temporarily stores on our computer. When we browse the website, the Web server will first send a small piece of information to the computer, and the cookie will record the text, behavior, or some choices we make on the website. When you access the same website again next time, the Web server first looks

What is cookie?

First, let's briefly introduce the cookie. Simply put, the cookie is the information that the server temporarily stores on our computer. When we browse the website, the Web server will first send a small piece of information to the computer, and the cookie will record the text, behavior, or some choices we make on the website. When you visit the same website next time, the Web server will first check whether there is any cookie information it left. If so, the Web server will judge the user based on the content in the cookie, send specific webpage content. Currently, cookie is most widely used to record user login information, so that you do not need to enter your username or password for the next visit, which is more convenient.

For more information about cookies, see Baidu encyclopedia http://baike.baidu.com/view/835.htm.

Cookie features

Cookie has four attributes: cookie lifecycle, path attribute, domain attribute, and cookie Security attribute.

Ookie lifecycle: expires. By default, the cookie only exists in the browser session period. After the browser is closed, the cookie does not exist. We can set the expiration time for the cookie through expires, after the time expires, the cookie will disappear and the cookie file will be automatically deleted.

Path attribute: by default, the cookie can be called in the same directory file. For example.

Domain attribute: If the domain is set to ".beyondweb.cn", all files on the server under ".beyondweb.cn" can call the same cookie.

In addition, cookies have some limitations. A browser can store up to 300 cookies. A single web server can only store up to 20 cookies, and each cookie cannot exceed 4000 bytes.

Create cookie in JS

We can use document. cookie to create a cookie.

1234567891011121314  New document    Document. cookie = "user = wangchi"; document. cookie = "pass = 1234" alert (document. cookie); script

Note: Some browsers need to create cookies in the server environment. You can use Firefox first.

We can run the above Code through the Firefox browser.

Then, right click → view page information → Security → view cookie so that we can see the cookie we saved, as shown in

However, when we restart the browser, the cookie disappears. This is because, by default, the browser closes and the cookie disappears. Next we will set its expiration time through expires.

1234567
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.