Home > Web Front-end > JS Tutorial > What are cookies? js manually creates and stores cookies_javascript tips

What are cookies? js manually creates and stores cookies_javascript tips

WBOY
Release: 2016-05-16 16:46:47
Original
1105 people have browsed it
What are cookies?

A cookie is a variable that is stored on a visitor's computer. This cookie is sent each time the same 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 a page for the first time, he or she may fill in his/her name. The name will be stored in a cookie. When visitors return to the site, they receive a welcome message like "Welcome John Doe!" The name is retrieved from the cookie.
Password cookie
When a visitor visits a page for the first time, he or she may fill in his/her password. Passwords can also be stored in cookies. When they visit the site again, the password is retrieved from the cookie.
Date cookie
When a visitor first visits your website, the current date can be stored in the cookie. When they visit the site again, they receive a message similar to this: "Your last visit was on Tuesday August 11, 2005!". The date is also retrieved from the cookie.

Creating and storing cookies

In this example we are going to create a cookie that stores the visitor's name. When visitors first visit the site, they are asked to fill in their name. The name will be stored in a cookie. When visitors return to the website, they receive a welcome message.

First, we will create a function that stores the visitor's name in a cookie variable:
Copy code The code is 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 the above function store the name, value and expiration days of the cookie.

In the above function, we first convert the number of days into a valid date, then, we store the cookie name, value and its expiration date into the document.cookie object.

After that, we want to create another function to check if the cookie has been set:
Copy code The code is as follows:

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 will first check whether there is a cookie in the document.cookie object. If the document.cookie object stores certain cookies, it will continue to check whether the cookie we specify has been stored. If the cookie we want is found, a value is returned, otherwise an empty string is returned.

Finally, we need to create a function. The function of this function is: if the cookie has been set, display the welcome message, otherwise display a prompt box to ask the user to enter a name.
Copy code The code is 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 the code:
Copy code The code is as follows:









Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template