Cookies are used to identify users.
Instance
-
Create a welcome cookie
-
Create a javascript cookie using the data entered in the prompt box. When the user visits the page again, send a welcome message based on the information in the cookie.
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 fill in 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 cookies. 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 and store cookies
In this example, we will create a cookie that stores the visitor's name. When visitors access the website for the first time, they are asked to enter their names. The name is stored in the cookie. When a visitor visits the website again, they will receive a welcome word.
First, we will create a function that stores the visitor's name in the 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 cookie name, value, and Expiration days.
In the above function, we first convert the number of days to a valid date, and then store the cookie name, value, and expiration date to the document. Cookie object.
Then, we will create another function to check whether the cookie has been set:
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 ""}
The above function first checks whether the document. Cookie object contains a cookie. If the document. Cookie object contains certain cookies, it will continue to check whether the specified cookie is saved. If the cookie is found, the return value is returned. Otherwise, an empty string is returned.
Finally, we need to create a function. The function is used to display the welcome word if the cookie has been set. Otherwise, a prompt box is displayed asking you 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: