JavaScript Cookies

JavaScript Cookies

Cookies are used to identify users.

<html>
<mate chatset="utf-8">
<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('请输入姓名:',"")
  if (username!=null && username!="")
    {
    setCookie('username',username,365)
    }
  }
}
</script>
</head>
<body onLoad="checkCookie()">
</body>
</html>

What is a cookie?

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 first visits a page, 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 can store 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 store the name, value and expiration date 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 need to 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 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 cookies we specify have 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.

function checkCookie()
{
username=getCookie('username')
if (username!=null && username!="")
  {alert('Welcome again '+username+'!')}
else 
  {
  username=prompt('请输入姓名:',"")
  if (username!=null && username!="")
    {
    setCookie('username',username,365)
    }
  }
}

This is all the code:

<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('请输入姓名:',"")
  if (username!=null && username!="")
    {
    setCookie('username',username,365)
    }
  }
}
</script>
</head>
<body onLoad="checkCookie()">
</body>
</html>


Continuing Learning
||
<html> <mate chatset="utf-8"> <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('请输入姓名:',"") if (username!=null && username!="") { setCookie('username',username,365) } } } </script> </head> <body onLoad="checkCookie()"> </body> </html>
submitReset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!