About cookies in JavaScript detailed description (1/7)

Source: Internet
Author: User

Cookie Overview
In the previous section, you used an immutable framework to store shopping bar data, and the product display page was constantly changing, although it was not rigorous to achieve a global variable. For example, if you right-click within the Navigation frames page and click Refresh on the shortcut menu, all the page effects variables will be lost. Therefore, to implement a strict cross page global variable, this approach is not possible, another mechanism in javascript: cookies, you can achieve the requirements of a true global variable.

A cookie is a mechanism provided by the browser that provides the cookie properties of the document object to JavaScript. It can be controlled by JavaScript, not by the nature of JavaScript itself. A cookie is a file that is stored on a user's hard disk, which usually corresponds to a domain name, which is available when the browser accesses the domain name again. As a result, cookies can span multiple pages under one domain name, but cannot be used across multiple domain names.

Different browsers implement cookies differently, but their properties are the same. For example, in Windows 2000 and Windows XP, cookie files are stored in the Documents and Settingsusernamecookie folder. The usual naming format is: Username@domain.txt.

The cookie mechanism stores information on the user's hard disk, so it can be used as a global variable, which is one of its greatest advantages. It can be used in several situations.

(1) Save the user login status. For example, a user ID is stored in a cookie so that the next time a user accesses the page, there is no need to log on again, and many forums and communities now offer such functionality. Cookies can also set the expiration time, and the cookie will automatically disappear when the time limit is exceeded. As a result, the system can often prompt the user to remain logged in: The common options are one months, three months, a year, and so on.

(2) Tracking user behavior. For example, a weather forecast site that can display local weather conditions according to the user's chosen area. If you need to choose the location every time is cumbersome, when the use of cookies will appear very user-friendly, the system can remember the last visit to the region, the next time you open the page, it will automatically show the last user area of the weather. Because everything is done in the background, such a page is as easy to use as it is customized for a particular user.

(3) custom page. If the site provides the ability to change the skin or replace the layout, you can use cookies to record the user's options, such as background color, resolution, and so on. You can still save the interface style of the last visit when the user accesses the next time.

(4) Create a shopping cart. Just as in the previous example, a cookie is used to record the goods the user needs to buy, which can be submitted uniformly at checkout time. Taobao, for example, uses cookies to record the goods that users have viewed, making it easier to compare them at any time.

Home 1 2 3 4 5 6 7 last
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.