JavaScript creating and storing cookies sample

Source: Internet
Author: User
Tags getdate setcookie

 javascript Create and store Cookie,cookie are variables that are stored on the visitor's computer, and take a look at the example.

What is a cookie? A cookie is a variable that is stored in a visitor's computer. This cookie is sent whenever the same computer requests a page through the browser. You can use JavaScript to create and retrieve a cookie's value.   Cookie Usage Scenario:   Name Cookie when the visitor first accesses the page, he or she may fill in his or her name. The name is stored in the cookie. When visitors visit the site again, they receive a welcome word like "Welcome John doe!". And the name is retrieved from the cookie.   Password Cookie When the visitor first accesses the page, he or she may fill in his or her password. Passwords can also be stored in cookies. When they visit the site again, the password is retrieved from the cookie.   Date Cookies The current date can be stored in a cookie when the visitor first visits your site. When they visit the site again, they receive a message similar to: "Your last Visit is on Tuesday August 11, 2005!". The date is also retrieved from the cookie.   Create and store cookies   In this example we want to create a cookie that stores the visitor's name. When visitors visit the site for the first time, they are asked to fill in their names. The name is stored in the cookie. When visitors visit the site again, they receive a welcome word.   First, we'll create a function that stores the visitor's name in the cookie variable:   Creating cookies is actually a string:   "Username=amosli;expires=mon, Dec 20 16:20:04 GMT "Then document.cookie= the string above."   Code as follows: function Setcookie (c_name,value,expiredays) {var exdate=new Date () exdate.setdate (exdate.getdate () + Expiredays) document.cookie=c_name+ "=" +escape (value) + ((expiredays==null)? ' ":"; expires= "+exdate.togmtstring ())}     The parameters in this function contain the name, value, and expiration days of the cookie.   in the above function, we first convert the number of days to a valid date, and then we save the cookie name, value, and its expiration date to the Document.cookie object. After  , we want to create another function to check if the cookie is set: The core of   GetCookie () is a word document.cookie, and the remainder is the processing of the string.     Code as follows: function GetCookie (c_name) {if (document.cookie.length>0)   {  C_start=document.cookie . indexOf (c_name + "=")/*  cookie is a set of strings: "USERNAME=AMOSLI; Aspsessionidqaaadbdd=ggijnhcdkgnfegjiifdnnion; __utma=119627022.986713705.1387205055.1387205055.1387208465.2; __utmb=119627022.3.10.1387208465; __utmc=119627022; __utmz=119627022.1387208465.2.2.utmcsr=google|utmccn= (organic) |utmcmd=organic|utmctr= (not%20provided) "*/if (c_ Start!=-1) {      C_start=c_start + c_name.length+1      C_end=document.cookie.indexof (";" , C_start)     if (c_end==-1) c_end=document.cookie.length     return unescape ( Document.cookie.substring (c_start,c_end))    }   } return ""}     above function firstA cookie is checked in the Document.cookie object before it is stored. If the Document.cookie object has certain cookies, it will continue to check that our specified cookie is stored. If we find the cookie we want, we return the value, otherwise we return an empty string.   Finally, we want to create a function that, if the cookie is set, displays the welcome word, otherwise a prompt box is displayed to require the user to enter a name.   Code as follows: function Checkcookie () {Username=getcookie (' username ') if (username!=null && username!= "")   { Alert (' Welcome again ' +username+ ')} else    {  username=prompt (' Please enter your name: ', ' ")   if (username!=null && username!=" "     {    Setcookie (' username ', username,365)    }  }}     This is all code: &NBSP ; The code is as follows: <html> <head> <script type= "Text/javascript" > Function GetCookie (c_name) {if ( document.cookie.length>0)   {  C_start=document.cookie.indexof (c_name + "=")   if (c_start!=-1)     {      C_start=c_start + c_name.length+1      C_end=document.cookie.indexof ( ";", C_start)     if (c_end==-1) C_end=document.cookiE.length     Return unescape (document.cookie.substring (c_start,c_end))    }   } return "" }   function Setcookie (c_name,value,expiredays) {var exdate=new Date () exdate.setdate (exdate.getdate () + Expiredays) document.cookie=c_name+ "=" +escape (value) + ((expiredays==null)? ' ': '; expires= ' +exdate.togmtstring ()}   function Checkcookie () {Username=getcookie (' username ') if (username!= Null && username!= "")   {alert (' Welcome again ' +username+ ')} else    {  Username=prompt (' P Lease Enter your name: ', ' "')   if (username!=null && username!=" ")     {    Setcookie (' Us Ername ', username,365)    }  } </script> </head>   <body onload= "Checkcookie ()" > </body> </html>
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.