Cookies are used to identify users. Instance
-
Create a welcome cookie
-
Creates a JavaScript Cookie with the data entered by the user in the prompt and, when the user accesses the page again, sends a welcome message based on the information in the Cookie.
What is a cookie?
A cookie is a variable stored on a visitor's computer. This cookie is sent whenever a page is requested by the same computer through a browser. You can use JavaScript to create and retrieve the value of a cookie.
Examples of cookies:
-
Name Cookie
-
When a visitor visits the page for the first time, he or she may fill out his/her name. The name is stored in a 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 a visitor visits the page for the first time, he or she may fill in his/her password. The password can also be stored in a cookie. When they visit the site again, the password is retrieved from the cookie.
-
Date Cookie
-
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 like this: "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 will 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 a 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 a cookie variable:
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 the above function contain the name of the cookie, the value, and the number of days to expire.
In the above function, we first convert the number of days to a valid date, and then we deposit the cookie name, value, and its expiration date into the Document.cookie object.
After that, we'll create another function to check if the cookie has been set:
function GetCookie (c_name) { if (Document.cookie.length>0 =document.cookie.indexof (c _name + "=" ) if (C_start!=-1 =c_start + c_name.length+1 C_end =document.cookie.indexo F (";" ,c_start) if (c_end==-1) C_end=document.cookie.length Unescape (document.cookie.substring (C_start,c_end))}} return ""
The above function first checks whether a cookie exists in the Document.cookie object. If the Document.cookie object has certain cookies, it will continue to check whether the cookie we specified is stored. If we find the cookie we want, we return the value, otherwise we return an empty string.
Finally, we'll create a function that shows a welcome word if the cookie is set, or a prompt box to ask the user to enter a name.
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 the code:
<HTML><Head><Scripttype= "Text/javascript">functionGetCookie (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+1C_end=Document.cookie.indexOf (";", C_start)if(C_end==-1) C_end=Document.cookie.lengthreturnunescape (document.cookie.substring (C_start,c_end))}}return ""}functionSetcookie (c_name,value,expiredays) {varexdate=NewDate () exdate.setdate (Exdate.getdate ()+expiredays) Document.cookie=C_name+ "=" +Escape (value)+((Expiredays==NULL) ? "" : "; expires="+exdate.togmtstring ())}functionCheckcookie () {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) } }}</Script></Head><BodyOnLoad= "Checkcookie ()"></Body></HTML>
JavaScript Learn the third day (JavaScript Cookies)