3 DISCLAIMER: THESE JAVASCRIPT FUNCTIONS ARE SUPPLIED 'AS IS', WITH
4 NO WARRANTY EXPRESSED OR IMPLIED. YOU USE THEM AT YOUR OWN RISK.
5 PAUL STEPHENS DOES NOT ACCEPT ANY LIABILITY FOR
6 ANY LOSS OR DAMAGE RESULTING FROM THEIR USE, HOWEVER CAUSED.
8 Paul Stephens' cookie-handling object library
11 2.0 - Introduces field names
12 2.1 - Fixes bug where undefined embedded fields[] elements weren't written to disk
16 TO USE THIS LIBRARY, INSERT ITS CONTENTS IN THE <HEAD> SECTION
17 OF YOUR WEB PAGE SOURCE, BEFORE ANY OTHER JAVASCRIPT ROUTINES.
19 (C) Paul Stephens, 2001-2003. Feel free to use this code, but please leave this comment block in. This code must not be sold, either alone or as part of an application, without the consent of the author.
22 function cookieObject(name, expires, accessPath) {
25 this.fieldSeparator = "#"
27 this.expires = expires
28 this.accessPath = accessPath
30 this.fields = new Array()
31 this.fieldnames = new Array()
32 if (arguments.length > 3) { // field name(s) specified
34 for (i = 3; i < arguments.length; i++) {
35 this.fieldnames[j] = arguments[i]
38 this.fields.length = this.fieldnames.length
44 this.remove = ucDelete
47 this.namepos = ucNamePos
52 function ucFieldGet(fieldname) {
53 var i = this.namepos(fieldname)
57 return "BadFieldName!"
61 function ucFieldPut (fieldname, fieldval) {
62 var i = this.namepos(fieldname)
65 i = this.fieldnames.length;
66 this.fieldnames[i] = fieldname;
69 this.fields[i] = fieldval
73 function ucNamePos(fieldname) {
75 for (i = 0; i < this.fieldnames.length; i++) {
76 if (fieldname == this.fieldnames[i]) {
85 var cookietext = this.name + "="
87 // concatenate array elements into cookie string
89 // Special case - single-field cookie, so write without # terminator
90 if (this.fields.length == 1) {
91 cookietext += escape(this.fields[0])
92 } else { // multi-field cookie
93 for (i= 0; i < this.fields.length; i++) {
94 cookietext += escape(this.fields[i]) + this.fieldSeparator }
98 // Set expiry parameter, if specified
99 if (this.expires != null) {
100 if (typeof(this.expires) == "number") { // Expiry period in days specified
102 var expiredate = new Date()
103 expiredate.setTime(today.getTime() + 1000*60*60*24*this.expires)
104 cookietext += "; expires=" + expiredate.toGMTString()
105 } else { // assume it's a date object
106 cookietext += "; expires=" + this.expires.toGMTString()
107 } // end of typeof(this.expires) if
108 } // end of this.expires != null if
110 // add path, if specified
111 if (this.accessPath != null) {
112 cookietext += "; PATH="+this.accessPath }
115 // alert("writing "+cookietext)
116 document.cookie = cookietext
122 var search = this.name + "="
123 var CookieString = document.cookie
124 if(CookieString == null) CookieString = "";
127 if (CookieString.length > 0) {
128 offset = CookieString.indexOf(search)
130 offset += search.length
131 end = CookieString.indexOf(";", offset)
132 if (end == -1) { // cookie is last item in the string, so no terminator
133 end = CookieString.length }
134 this.rawValue = CookieString.substring(offset, end)
139 if (this.rawValue != null) { // unpack into fields
141 var sl = this.rawValue.length
146 // Special case - single-field cookies written by other functions,
147 // so without a '#' terminator
149 if (this.rawValue.substr(sl-1, 1) != this.fieldSeparator) {
150 this.fields[0] = unescape(this.rawValue)
151 } else { // separate fields
155 endidx = this.rawValue.indexOf(this.fieldSeparator, startidx)
157 this.fields[i] = unescape(this.rawValue.substring(startidx, endidx))
159 startidx = endidx + 1}
161 while (endidx !=-1 & endidx != (this.rawValue.length -1));
163 } // end of unpack into fields if block
168 function ucDelete() {
177 *********** IT'S OK TO REMOVE THE CODE BELOW HERE IF YOUR PAGE
178 DOESN'T USE cookieList() OBJECTS OR THE findCookieObject() FUNCTION.
184 function findCookieObject(cName, cObjArray) {
186 This function finds a named cookie among the objects
187 pointed to by a cookieList array (see below).
189 Parameters are the cookie name to search for (a string), and an array created with
190 the new cookieList() constructor (see below)
192 NOTE - if you're only dealing with a specific, named cookie, then it's
193 more efficient to ceate a single cookieObject directly with that name,
194 and check its .found property to see if it already exists on this client.
196 This function is for when you've created an all-cookies array anyway,
197 and now want to check whether a specific cookie is present.
199 It returns a pointer to the cookieObject if found, or null if not found.
202 var cpointer = null, i
203 for (i in cObjArray) {
204 if (cName == cObjArray[i].name) {
205 cpointer = cObjArray[i]
212 function cookieList() {
214 This constructor function creates a cookieObject object (see below)
215 for each cookie in document.cookie,
216 and returns an array of pointers to the objects.
218 You can use it to load all the cookies available to a page, then walk through them.
222 cookList = new cookieList()
223 for (i in cookList) {
224 document.write(cookList[i].name + " " + cookList[i].fields[0] + "
230 var i = 0, rawstring, offset = 0, start, newname
231 cpointers = new Array()
232 rawstring = document.cookie
233 if (rawstring.length > 0) {
235 start = rawstring.indexOf("=", offset)
236 if (start != -1) { // another cookie found in string
237 // get cookie string up to end of current cookie name
238 newname = rawstring.substring(0, start)
240 // if not first cookie in string, remove previous cookie data from substring
241 // subsequent cookie names have a space before them (just a little browser foible!)
242 newname = newname.substring(newname.lastIndexOf(";")+2, start)
244 cpointers[i] = new cookieObject(newname)
248 } while (start != -1)
249 } // end rawstring.length > 0