JavaScript Editor JavaScript Validator     JavaScript Editor 



Team LiB
Previous Section Next Section

Cookie Limitations

You should be aware of a number of limitations when using cookies.

The first is that while all modern browsers support cookies, the user may have disabled them. In NN we can do this from the Preferences option on the Edit menu. The option to disable cookies is under Advanced on NN 4.x, and under the Cookies option under Advanced on NN 6. In IE it's under Internet Options on the Tools menu. Select the Security tab and click the Custom Level button. From there, a list of security options appears, some of which relate to the setting of cookies.

Both the functions that we've created 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 we need to make sure our code can cope with this.

We could set a default action for when cookies are disabled. For example, in the previous example, if cookies are disabled, a What's New image will never appear.

Alternatively, we can let the user know that our website needs cookies to function by putting a message in the web page warning users.

Another way is to actively check to see whether cookies are enabled and, if not, take some action to cope with this, such as directing the user to a page with less functionality that does not need cookies. How do we check to see if cookies are enabled?

In the following script we set a test cookie, and then read back its value. If it's null, we know cookies are disabled.

setCookie("TestCookie","Yes","","");
if (getCookieValue("TestCookie") == null)
   {
      alert("This website requires cookies to function");
   }

A second limitation is on the number of cookies we can set on the user's computer for our website and how much information can be stored. For each domain and path we can store up to 20 cookies, and each cookie pair, that is, the name and value combined, must be not more than 4096 characters in size. It's also important to be aware that all browsers do set some upper limit for the number of cookies stored. Once that limit is reached, often what happens is older cookies, regardless of expiration date, are deleted. Modern browsers have a 300-cookie limit, though this may vary between browsers.

To get around the 20-cookie limit, we 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","","")

We 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 semicolons separating the parts of the cookie. When we get the cookie value back using getCookieValue(), we just split it into its constituent parts; however, we must remember the order we stored it in.

var cookieValues = getCookieValue("UserDetails");
cookieValues = cookieValues.split(";")
alert("Name = " + cookieValues[0]);
alert("Age = " + cookieValues[1]);
alert("Last Visit = " + cookieValues[2]);

Now we have acquired three pieces of information and still have 19 cookies left in the jar.


Team LiB
Previous Section Next Section


JavaScript Editor JavaScript Validator     JavaScript Editor


©

R7