A Cookie Toolbox

I have just been working on my first major project that uses cookies and made some discoveries about cookies that I haven't seen documented anywhere so I'll start by sharing with you what I found out.

For this project I wanted to store lots of values in cookies. I wrote my code and started testing. When I tried testing with lots of entries to be saved I found that I was losing values. It seems that you can only store about twenty different values in cookies and once you go past that the oldest ones are automatically removed. I solved this by coming up with a way to store arrays of values.

In trying to text the page on various browsers on my own computer, I noticed that while the code worked okay on Internet Explorer and Netscape (both 4 and 6) that it didn't seem to work on Opera. Fortunately, I have a web server installed on my own computer and when I accessed the page via the server instead of directly, the cookies did work after all.

Once I had figured out how to get what I wanted, I decided to start over and rewrite the code properly. As a result of this, I found that I had developed a cookie toolbox javascript that contains all of the cookie functions in one place. This will speed up your cookie projects for you so I am going to share it with you. First download the cookie toolbox javascript and save it to your site. You wont need to touch the code in this script at all so I am not going to discuss the code. Instead, I will tell you about each of the functions contained within the script and how to use them.

The first step is to link the script into your page by placing the following code in the head section of your page:


The page now has access to use all of the functions within the cookie toolbox. Let's start with the basic cookie functions.

set_cookie(name, value, expires);

This function allows you to create a cookie called name and store value in it. All cookies have an expiry date to stop them hanging around forever. You specify the expiry date for this cookie in the expires field. If you don't specify an expiry date then the function will use the current date and time and the cookie will expire at the end of your visitor's browser session.

value = get_cookie(name);

This function allows you to retrieve the content of a previously saved cookie called name into a field called value.


This function deletes the cookie called name.

As I mentioned at the start of this discussion, I came up with a way of storing arrays in cookies and the following functions will allow you to do the same.

myarray = init_array();

This initializes an array called myarray that will be used with the cookie array functions. You will load the values that you want to save in your cookie into this array before saving the cookie and when you retrieve the cookie the array will be cleared and then loaded with the values from the cookie. Provided that you are only dealing with one cookie at a time, you will only need one array as it can be reused. Note that you need to initialize your array before you call any of the following functions.

set_array(name, myarray, expires);

This function works the same as the set_cookie function except that it first converts the array into a single value field before storing it in the cookie. Note that this function uses the caret (^) as a field separator in this conversion so you will need to ensure that your array entries do not contain that character.

get_array(name, myarray);

This function will retrieve the cookie called name then convert the value field back into the array myarray.

del_entry(name, myarray, pos, expires);

This function allows you to delete an array entry from within the cookie called name. The content of the cookie will be retrieved into myarray and then the cookie will be recreated without the pos entry. A new expiry date of expires will be set.

size = next_entry(myarray);

The array that the toolbox uses dynamically grows and shrinks to meet your current requirements. This function lets you know that the myarray[size] entry is the first empty entry. That's the one you'll want to use when adding more entries into the array.

Given all of the trouble that I had in testing my javascript, with cookies disappearing unexpectedly, etc. I also built a debugging option into these functions as well as a way to check all of your saved cookies.


If you place this statement anywhere in your javascript then any subsequent calls to any of the above cookie functions will first produce an alert telling you which function is to be executed.


You can use this to turn the alerts off again at the end of the section of code that you are debugging so that subsequent calls to the cookie functions will no longer produce alerts.


This function can be used to place a raw copy of all of your cookies at the current location on your page so that you can see exactly what you currently have stored in cookies. This function can only be used in the body section of your page.

So there you are, a complete set of cookie functions that you can use with your site. If you require more detailed instructions on how to use the cookie toolbox then read Using the Cookie Toolbox.


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow