22. JavaScript Cookies

JavaScrript Cookies

What is Cookie ?
Create and Store a Cookie

A cookie is often used to identify a user

What is a Cookie ?

A cookie is a variable that is stored on the visitors computer.
Each time the same computer requests a page with a browser, it sends the cookie.

with Javascript youcan create and retrive cookie values

Examples of cookies:

Named cookie.
The first time visitor arrives on your wepage,
she must fill in her name. the name then is stored in a cookie.
Next time the visitor arrives at your page, she could get a welcome message like
"Welecome Jane Doe!" the name is retrived from second cookie

Password cookie.
The first time a visitor arrives on your webpage, she must fill in a password.
the password then is stored in a cookie.
Next time visitor arrives at your page, the password is retrived from the cookie.

Date cookie
The first time the visitor arrives to your webpage, the current date is stored in a cookie.
Next time the visitor arrives at your page, she could get a message like
"Your last visit was on Tuesday,August 11,2005!"
The date is retrived from the stored cookie.

Create and Store a Cookie

First, we create a function that stores the name of the visitor 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());
}

We first convert the number of days to a valid date and then we add the number of days until the cookie should expire.
After that, we store the cookie name,cookie value, and the expiration date in the document.cookie object

Then we create another function that checks whether the cookie has been set:
function getCookie(c_name)
{
if(document.cookie.length>0)
{
c_start=document.cooke.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 preceding function first checks whether a cookie is stored at all in the document.cookie object.
If the document.cookie object holds some cookies, then check to see whether our specific cookie is stored.
if  our cookie is found, then return the values;
if not, return an empty string

Last we create the function that displays a welcom message if the cookie is set, and if the cookie is not set,
it displays a prompt box asking for the name of the user:

function checkCookie()
{
username=getCookie('username');
if(username!=null && username!=" ")
{
alert('welcom agin' +username+ '!');
}
else
{
username=prompr('plese enter your name:'," ");
if(username!=null && username!=" ")
{
setCookie('username',username,365);
}
}
}

<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,exxpiredays)
{
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>
</head>
<body onLoad="checkCookie()">
</body>
</html>

No comments:

My Favorite Books

  • C and Data Structures by Ashok N. kamthane
  • Web Technologies by A. A. Puntambekar
  • Learn HTML and CSS with W3Schools
  • Learn JavaScript and Ajax with W3Schools
  • HTML5 Black Book: Covers Css3, Javascript,XML, XHTML, Ajax, PHP And Jquery
  • HTML5 Application Development Fundamentals: MTA Exam 98-375
  • .NET 4.0 Programming 6-In-1, Black Book
  • SQL Server 2008 R2 Black Book
  • Asp.net 4.0 Projects Covers: net 3.5 And .net 4.0 Codes, Black Book
  • UNIX Shell Programming 1 Edition by Yashavant Kanetkar
  • UNIX and Shell Programming 1 Edition by Richard F. Gilberg, Behrouz A. Forouzan
  • Computer Networks by Andrew S. Tanenbaum
  • Multiple Choice questions in computer science by Timothy J Williams