The instance learns JavaScript to read and write cookies, and uses javascriptcookie.

Source: Internet
Author: User

The instance learns JavaScript to read and write cookies, and uses javascriptcookie.

First, let's take a brief look at cookies.

In the process of webpage creation, we often need to pass information from one page to another. At this time, we need to use the cookie Mechanism in JavaScript. Simply put, cookies provide a convenient way to store a small amount of data on users' computers and obtain them remotely, so that the website can save some details, for example, the user's habits are set or the last time the website was accessed. Cookies are short information that can be stored on the user's computer and read by other pages. Cookies are generally set to expire after a certain period of time.

Of course, there are limits on the number of cookies that a browser can store, usually several hundred or more. Generally, 20 cookies per domain name are allowed, and each domain can store up to 4 kb of cookies. in addition to the size limit, there are also many causes that may cause the disappearance of cookies on the hard disk. For example, when the validity period is reached, or the user clears the cookie information, or another browser is used. Therefore, cookies are not suitable for storing important data. When writing code, you must also consider how to handle exceptions obtained by cookies.

In JavaScript, the cookie attribute of the document object is used to store and obtain cookies. generally, document. the information in the cookie is a string consisting of pair names and values. The form of each pair of data is:

name=value;

The following is a simple example to show how to read and write cookies in JavaScript.

The first step is createcookie.html. A cookie is created on this page. The complete code is as follows:

The page is as follows:

Click "submit" to create a cookie, store usernameand passwordinformation in the cookie, and access the showcookie.html page. The complete code for the showcookie.html page is as follows:

The page is as follows:

Click show Cookie to display the information in the cookie.

Next, we will run this program locally and on the server, respectively, on IE and Chrome respectively, to check the cookie running status.

First, we run this program locally. We put the above two files on the edisk. Run on the IE browser, enter information on the createcookie.html page, and click the submit button, as shown below:

Go to the showcookie.html page and click show Cookie. The page is as follows:

The cookie runs normally in IE browser in the local environment.

Next, let's see how the Chrome browser is running. Open the createcookie.html page in the Chrome browser first, enter the information, and click the submit button, as shown below:

Go to the showcookie.html page and click show Cookie. The page is as follows:

In the same program, the cookie failed to run in the Chrome browser.

Then let's run this program on the server. xampp is required and the Apache server is enabled, place the preceding two html files in the htdocs folder under the xampp installation folder (for detailed operation methods, refer to this blog: AJAX for JavaScript (suitable for beginners )). We are now running this program in IE browser. in IE browser, enter the URL: http: // localhost/createCookie ...:

Click submit To Go To The showcookie.html page, and click show Cookie, as shown below:

Then, we enter the URL http: // localhost/createCookie In the Chrome browser ...:

Click submit To Go To The showcookie.html page, and click show Cookie, as shown below:

In this server environment, both IE and Chrome cookies run normally!

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.