How does JavaScript read and write cookies?

Source: Internet
Author: User
Tags setcookie

Today, I reviewed how javascript is used to create and store cookies. I would like to share some of my experiences with you. First, let's take a look at the basic knowledge:

What is cookie?

Cookie is a variable stored on the visitor's computer. This cookie is sent every time a computer requests a page through a browser. You can use JavaScript to create and retrieve cookie values.

Examples of cookies
  • Name cookie: When a visitor visits the page for the first time, he or she may enter his/her name. The name is stored in the cookie. When a visitor visits the website again, they will receive a message similar to "Welcome John Doe! . The name is retrieved from the cookie.
  • Password cookie: When a visitor visits the page for the first time, he or she may fill in his/her password. Passwords can also be stored in cookies. When they access the website again, the password will be retrieved from the cookie.
  • Date cookie: When a visitor visits your website for the first time, the current date can be stored in the cookie. When they access the website again, they will receive a message like this: "Your last visit was on Tuesday August 11,200 5! ". The date is also retrieved from the cookie.

Create a cookie instance, how to create a cookie, and how to retrieve the cookie.

Javascript code:

// Create cookiefunction setCookie (name, value, expireday) {var exp = new Date (); exp. setTime (exp. getTime () + expireday * 24*60*60*1000); // set the cookie term document. cookie = name + "=" + escape (value) + "; expires" + "=" + exp. toGMTString (); // create cookie} // extract the value of function getCookie (name) {var cookieStr = document. cookie; if (cookieStr. length> 0) {var cookieArr = cookieStr. split (";"); // convert cookie information into an array for (var I = 0; I <cookie Arr. length; I ++) {var cookieVal = cookieArr [I]. split ("="); // convert each cookie (cookie name and value) into an array if (cookieVal [0] = name) {return unescape (cookieVal [1]); // return the cookie value to be extracted }}// test cookiefunction checkCookie () {var cookieUser = document. getElementById ("cookieUser"); var userName = getCookie ("userName"); if (userName) {cookieUser. innerHTML = "hello" + userName + ", welcome back again! ";}Else {var value = prompt (" Enter the user name "," "); if (value) {setCookie ('username', value, 1 );} else {alert ("Enter the user name! ");}}}

The main difference is how to extract the cookie information we need. In this example, the getCookie function converts the cookie information into an array to find the cookie value we need to extract. You can also use the regular expression to match, as shown below:

function getCookie(name) {var cookieStr = document.cookie;var cookieArr = cookieStr.match(new RegExp(name+"=[a-zA-Z0-9]*;$"));var cookieVal = cookieArr.split("=");if(cookieVal[0] == name) {return unescape(cookieVal[1]);}}

For example, in this example, if the cookie named userName is not stored in the browser, the user is prompted to enter the user name. When the page is refreshed again, the entered cookie value is displayed. Finally, we can test the Code:

<body onload="checkCookie()"><p id="cookieUser"></p></body>

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.