What is COOKIE?JS manual creation and storage Cookie_javascript tips

Source: Internet
Author: User
Tags getdate setcookie
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.
Examples of cookies:

Name Cookie
When a visitor visits the page for the first time, 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 a visitor accesses the page for the first time, 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 Cookie
When a visitor visits your site for the first time, the current date can be stored in a cookie. 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.

creating and storing 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:
Copy Code code as follows:

<span style= "FONT-SIZE:14PX;" >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 ())
}</span>

The parameters in the above function hold 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 that, we'll create another function to check if the cookie is set:
Copy Code code as follows:

<span style= "FONT-SIZE:14PX;" >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 ""
}</span>

The above function first checks to see if a cookie exists in the Document.cookie object. 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're going 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.
Copy Code code as follows:

<span style= "FONT-SIZE:14PX;" >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)
}
}
}</span>

This is all the code:
Copy Code code as follows:

<span style= "FONT-SIZE:14PX;" ><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 (' Please enter your name: ', "")
if (username!=null && username!= "")
{
Setcookie (' username ', username,365)
}
}
}
</script>

<body onload= "Checkcookie ()" >
</body>

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.