Java Reference
In-Depth Information
The rest of the fi rst script block contains the two functions getCookieValue() and setCookie() that
you looked at earlier. These haven't changed, so they're not discussed in detail here.
The interesting material is in the second script block within the body of the page. First you get the date
of the user's last visit from the LastVisit cookie using the getCookieValue() function.
var lastVisit = getCookieValue(“LastVisit”);
If it's null, the user has either never been here before, or it has been six or more months since the last
visit and the cookie has expired. Either way, you won't put a What's New image up because everything
is new if the user is a fi rst-time visitor, and a lot has probably changed in the last six months — more
than what your What's New page will detail.
If lastVisit is not null , you need to check whether the user visited the site before it was last updated,
and if so to direct the user to a page that shows what is new. You do this within the if statement.
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>'
}
}
You fi rst create a new Date object based on the value of lastVisit and store that back into the
lastVisit variable. Then, in the condition of the inner if statement, you compare the date of the user's
last visit with the date on which you last updated the web site. If things have changed since the user's last
visit, you write the What's New image to the page, so the user can click it and fi nd out what's new.
Finally, at the end of the script block, you reset the LastVisit cookie to today's date and time using
the setCookie() function.
var nowDate = new Date();
setCookie(“LastVisit”, nowDate.toGMTString(),””,””)
Cookie Limitations
You should be aware of a number of limitations when using cookies.
A User May Disable Cookies
The fi rst limitation is that although all modern browsers support cookies, the user may have disabled
them. In Firefox you can do this by selecting the Options menu, followed by the privacy tab and the
cookies tab. In IE you select Internet Options on the Tools menu. Select the Privacy tab and you can
change the level with the scroll control. Most users have session cookies enabled by default. Session
cookies are cookies that last for as long as the user is browsing your web site. After he's closed the
Search WWH ::




Custom Search