Java Reference
In-Depth Information
You first 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 website. If things have changed since the
user's last visit, you write the What's New? link to the page, so the user can click it and find out what's
new. Finally, at the end of the script you reset the LastVisit cookie to today's date and time using the
setCookie() function:
var now = new Date();
setCookie("LastVisit", nowDate.toUTCString());
Cookie limitations
You should be aware of a number of limitations when using cookies.
a user may disable Cookies
The first 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 gear menu. Select the Privacy tab and
you can change the level with the scroll control. And in Chrome, choose the Settings option from
the gear menu, search for cookies, and click “Content settings.” Most users have session cookies
enabled by default. Session cookies are cookies that last for as long as the user is browsing your
website. After he's closed the browser the cookie will be cleared. More permanent cookies are also
normally enabled by default. However, third‐party cookies, those from a third‐party site, are usually
disabled. These are the cookies used for tracking people from site to site and hence the ones that
raise the most privacy concerns.
Both the functions that you've made for creating and getting cookies will cause no errors when
cookies are disabled, but of course the value of any cookie set will be null and you need to make
sure your code can cope with this.
You could set a default action for when cookies are disabled. In the previous example, if cookies are
disabled, the What's New? link will never appear.
Alternatively, you can let the user know that your website needs cookies to function by putting a
message to that effect in the web page.
Another tactic is to actively check to see whether cookies are enabled and, if not, to take some
action to cope with this, such as by directing the user to a page with less functionality that does not
need cookies. How do you check to see if cookies are enabled?
In the following script, you set a test cookie and then read back its value. If the value is null , you
know cookies are disabled:
setCookie("TestCookie","Yes");
if (!getCookieValue("TestCookie")) {
alert("This website requires cookies to function");
}
 
Search WWH ::




Custom Search