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)
64 this.fields[i] = fieldval
71 function ucNamePos(fieldname) {
73 for (i = 0; i < this.fieldnames.length; i++) {
74 if (fieldname == this.fieldnames[i]) {
83 var cookietext = this.name + "="
85 // concatenate array elements into cookie string
87 // Special case - single-field cookie, so write without # terminator
88 if (this.fields.length == 1) {
89 cookietext += escape(this.fields[0])
90 } else { // multi-field cookie
91 for (i= 0; i < this.fields.length; i++) {
92 cookietext += escape(this.fields[i]) + this.fieldSeparator }
96 // Set expiry parameter, if specified
97 if (this.expires != null) {
98 if (typeof(this.expires) == "number") { // Expiry period in days specified
100 var expiredate = new Date()
101 expiredate.setTime(today.getTime() + 1000*60*60*24*this.expires)
102 cookietext += "; expires=" + expiredate.toGMTString()
103 } else { // assume it's a date object
104 cookietext += "; expires=" + this.expires.toGMTString()
105 } // end of typeof(this.expires) if
106 } // end of this.expires != null if
108 // add path, if specified
109 if (this.accessPath != null) {
110 cookietext += "; PATH="+this.accessPath }
113 // alert("writing "+cookietext)
114 document.cookie = cookietext
120 var search = this.name + "="
121 var CookieString = document.cookie
124 if (CookieString.length > 0) {
125 offset = CookieString.indexOf(search)
127 offset += search.length
128 end = CookieString.indexOf(";", offset)
129 if (end == -1) { // cookie is last item in the string, so no terminator
130 end = CookieString.length }
131 this.rawValue = CookieString.substring(offset, end)
136 if (this.rawValue != null) { // unpack into fields
138 var sl = this.rawValue.length
143 // Special case - single-field cookies written by other functions,
144 // so without a '#' terminator
146 if (this.rawValue.substr(sl-1, 1) != this.fieldSeparator) {
147 this.fields[0] = unescape(this.rawValue)
148 } else { // separate fields
152 endidx = this.rawValue.indexOf(this.fieldSeparator, startidx)
154 this.fields[i] = unescape(this.rawValue.substring(startidx, endidx))
156 startidx = endidx + 1}
158 while (endidx !=-1 & endidx != (this.rawValue.length -1));
160 } // end of unpack into fields if block
165 function ucDelete() {
174 *********** IT'S OK TO REMOVE THE CODE BELOW HERE IF YOUR PAGE
175 DOESN'T USE cookieList() OBJECTS OR THE findCookieObject() FUNCTION.
181 function findCookieObject(cName, cObjArray) {
183 This function finds a named cookie among the objects
184 pointed to by a cookieList array (see below).
186 Parameters are the cookie name to search for (a string), and an array created with
187 the new cookieList() constructor (see below)
189 NOTE - if you're only dealing with a specific, named cookie, then it's
190 more efficient to ceate a single cookieObject directly with that name,
191 and check its .found property to see if it already exists on this client.
193 This function is for when you've created an all-cookies array anyway,
194 and now want to check whether a specific cookie is present.
196 It returns a pointer to the cookieObject if found, or null if not found.
199 var cpointer = null, i
200 for (i in cObjArray) {
201 if (cName == cObjArray[i].name) {
202 cpointer = cObjArray[i]
209 function cookieList() {
211 This constructor function creates a cookieObject object (see below)
212 for each cookie in document.cookie,
213 and returns an array of pointers to the objects.
215 You can use it to load all the cookies available to a page, then walk through them.
219 cookList = new cookieList()
220 for (i in cookList) {
221 document.write(cookList[i].name + " " + cookList[i].fields[0] + "
227 var i = 0, rawstring, offset = 0, start, newname
228 cpointers = new Array()
229 rawstring = document.cookie
230 if (rawstring.length > 0) {
232 start = rawstring.indexOf("=", offset)
233 if (start != -1) { // another cookie found in string
234 // get cookie string up to end of current cookie name
235 newname = rawstring.substring(0, start)
237 // if not first cookie in string, remove previous cookie data from substring
238 // subsequent cookie names have a space before them (just a little browser foible!)
239 newname = newname.substring(newname.lastIndexOf(";")+2, start)
241 cpointers[i] = new cookieObject(newname)
245 } while (start != -1)
246 } // end rawstring.length > 0