Java Reference
In-Depth Information
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 image will never appear.
Alternatively, you can let the user know that your web site needs cookies to function by putting a mes-
sage 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 cook-
ies. 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”) == null)
{
alert(“This website requires cookies to function”);
}
Number and Information Limitation
A second limitation is on the number of cookies you can set on the user's computer for your web site and
how much information can be stored in each. In older browsers for each domain it was common you
could store only up to 20 cookies, and each cookie pair — that is, the name and value of the cookie com-
bined — must not be more than 4,096 characters in size. It's also important to be aware that all browsers
do set some upper limit for the number of cookies stored. When that limit is reached, older cookies,
regardless of expiration date, are often deleted. Modern, for example IE7+ and Firefox browsers have
a 50-cookie limit, though this may vary between browsers.
To get around the cookie limits, you can store more than one piece of information per cookie. This
example uses multiple cookies:
setCookie(“Name”,”Karen”,””,””)
setCookie(“Age”,”44”,””,””)
setCookie(“LastVisit”,”10 Jan 2001”,””,””)
You could combine this information into one cookie, with each detail separated by a semicolon.
setCookie(“UserDetails”,”Karen;44;10 Jan 2001”,””,””)
Because the setCookie() function escapes the value of the cookie, there is no confusion between the
semicolons separating pieces of data in the value of the cookie, and the semicolons separating the parts
Search WWH ::




Custom Search