Remove tests
-----------------------------------------------------------------------------------------------------------
Straight
Create Cookie Movie:
Erase Cookie Movie:
-----------------------------------------------------------------------------------------------------------
Function
Create Cookie Movie:
Erase Cookie Movie:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
The whole entry is supplied as one quoted String with segments set apart with semicolons — first the name-value pair, then the expiry date in the correct format, and finally the path. This syntax is fixed, and you shouldn't go rearranging the elements. Write this entry now. To test out the cookie's contents, we can use a simple script like
alert(document.cookie);
Which will yield this result. Write#2 entry 2
document.cookie = "testvalue2=Nah; expires=Fri, 13 Jul 2004 05:28:21 UTC; path=/";
Checking on the cookie's contents now, we can see that our first value is still in there. Had we used the same name, the first value would've been overwritten; but since we used a different name, the new entry has been added in with the first.
Erasing cookie entriesdocument.cookie= "testvalue2=Whatever; expires=Fri, 13 Jul 2001 05:28:21 UTC; path=/";You can also give the entry an expiry date of
-1
, and it will be erased immediately. Erase test values 1 and 2 now, if you have the heart.Convenient Scripts
To easily tinker with cookies ourselves, we'll be using some great scripts which were originally coded by » Scott Andrew. They'll take much of the pain out of the process; especially reading the values out of a cookie, which is a bit complicated. Here are the functions:
function createCookie(name, value, days) { if (days) { var date = new Date(); date.setTime(date.getTime()+(days*24*60*60*1000)); var expires = "; expires="+date.toGMTString(); } else var expires = ""; document.cookie = name+"="+value+expires+"; path=/"; } function readCookie(name) { var ca = document.cookie.split(';'); var nameEQ = name + "="; for(var i=0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length); } return null; } function eraseCookie(name) { createCookie(name, "", -1); }These are some nicely coded scripts, and don't require too much explanation. The function we use to create cookies takes three arguments, which make up the name-value pair and the amount of days to retain the cookie. The last argument is converted into a valid date by adding its value in hours to the current time before being annexed into the line which creates the cookie.
The cookie reading function is the most difficult one here. First it splits the available cookie String (what we've been reading out in the alert earlier on this page) at every occurrence of the separating semicolon. This creates a new array, with each index holding an entry pair. We loop through these looking for the String '
name=
'. When we find this, we read out whatever else makes up this index, which will be the value associated with the name we passed to the function at the beginning.Erasing an entry is easy — simply recreate a cookie with its expiration date set to
-1
.++++++++++++++++++++++++++++++++++++++++++