Cookie Toolbox2. Cookie Functions |
|
Join the DiscussionMore of this FeatureRelated ResourceWith the cookie toolbox script linked into the page we now has access to use all of the functions within the cookie toolbox. Let's look at the available functions one by one. Descriptions of what each function does follows each function template.
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.
del_cookie(name);
This function deletes the cookie called name.
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.
debug_on();
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.
debug_off();
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.
dump_cookies();
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 now that you know what functions the toolbox provides, all that is left is to see how we use these tools to store, retrieve, and delete cookies.
The Script | Cookie
Functions | Using the Tools
Note that the script and the descriptions of the functions that it contains are copied from the ">Ask Felgall" website with the permission of the copyright owner. |

