[ acky.net logo ]

IRCd's Starting as Low as $10, Great Reputation
IRCd's Starting as Low as $10, Great Reputation
Home > Tutorials > JavaScripts > Save Form Data With Cookies

Save Form Data With Cookies
By Alex Osipov
LAST EDITED: Monday, April 16, 2001 3:54 AM

    Now that you are sure to know what cookies are and how they work we will try to save the cookie via JavaScript. First we need the expire date before saving the cookie. We create a data object and set the date to be exactly 1 year from the user's current time. What this means is that the cookie will be deleted and be no longer valid in 1 year. Now that we have the expire date, the cookie value (we set that before if you still remember), and we also have the global variables that we set in the beginning to use in saving the cookie. We set the cookie by setting the variable document.cookie to equal the expected cookie structure (mentioned before). After this we are done, and all we have to do is simply bust out of the function (line 54).

    Now that was relatively pain-free, wasn't it? The retrieve_field function is going to be even easier. The retrieve_field function does the opposite of what the save_field function does. First we determine the type of object we are going to be handling, as most of the objects are going to be handled in a different way. Now we must parse out the value of the cookie sent by the browser to the JavaScript. This value is stored in document.cookie. First we must find the beginning of the object name and the string "=[", which will be the beginning index of the object state value that we are trying to retrieve (however, we must add the length of the object name and 2 to make up for the length of the object name and the string "=[", and get the real beginning) (lines 66-68). We now determine where the ending delimiter, the string "]", of the cookie ends (line 70). Now that we have the beginning and the end of the value we need we simply use the String method substring and retrieve the value (line 71). That's it. We have everything we need to restore the state the user last left the field in. We go through saving the field as we did in save_field but in reverse instead of retrieving the state we now save it.

    That is all that we need to do. There are a few things to remember about using cookies with JavaScript. The first and probably most important is that not all browsers handle JavaScripts and therefore you cannot rely on using JavaScript to set cookies in critical situations. The second is that the browser places a limit on the amount and size of cookies one domain can have, so try to restrict yourself in saving data. I as a non believer in users do not like leaving much to the users, and therefore do not like using cookies with JavaScript in most situations except those that require and call for JavaScript. Consider carefully before you use this script, and remember there are other ways of accomplishing the same thing. To save form data you can use other technologies such as the formData technology from Microsoft, or even server-side cookies. Choose whatever suits your situation.

 

 

Email This Page To A Friend

Last updated: Monday, November 27, 2006 - 11:02 AM Eastern Daylight Time
Legal | Privacy Statement | Problems & Questions | Advertise | Link to us
© 1997-2007, All Rights Reserved.


Dedicated Server Provided by HighSpeedHosting