Java Reference
In-Depth Information
Try It Out What's New?
Now you know how to create and retrieve cookies. Let's use this knowledge in an example in which
you check to see if any changes have been made to a web site since the user last visited it.
You'll be creating two pages for this example. The fi rst is the main page for a web site; the second is the
page with details of new additions and changes to the web site. A link to the second page will appear
on the fi rst page only if the user has visited the page before (that is, if a cookie exists) but has not visited
since the page was last updated.
Let's create the fi rst page.
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title>Cookie Example</title>
<script language=”JavaScript” type=”text/JavaScript”
src=”CookieFunctions.js”></script>
<script language=”JavaScript” type=”text/javascript”>
var lastUpdated = new Date(“Tue, 28 Dec 2020”);
</script>
</head>
<body>
<h1 align=center>Welcome to my website</h1>
<div align=”center” id=”WhatsNewDiv”></div>
<script>
var lastVisit = getCookieValue(“LastVisit”);
if (lastVisit != null)
{
lastVisit = new Date(lastVisit);
if (lastVisit < lastUpdated)
{
document.getElementById('WhatsNewDiv').innerHTML = '<a
href=”WhatsNew.htm”><img src=”WhatsNew.jpg” border=0></a>'
}
}
var nowDate = new Date();
setCookie(“LastVisit”, nowDate.toGMTString(),”“,”“)
</script>
<body>
</body>
</html>
This page needs to be saved as MainPage.htm. Note that it contains the two functions, setCookie()
and getCookieValue(), that you created earlier. Also note that the image WhatsNew.jpg is referenced
by this page; either create such an image, or retrieve the image from the code download.
Next, you'll just create a simple page to link to for the What's New details.
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
Search WWH ::




Custom Search