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
88 // Special case - single-field cookie, so write without # terminator
89 if (this.fields.length == 1) {
90 cookietext += escape(this.fields[0])
91 } else { // multi-field cookie
92 for (i= 0; i < this.fields.length; i++) {
93 cookietext += escape(this.fields[i]) + this.fieldSeparator }
97 // Set expiry parameter, if specified
98 if (this.expires != null) {
99 if (typeof(this.expires) == "number") { // Expiry period in days specified
101 var expiredate = new Date()
102 expiredate.setTime(today.getTime() + 1000*60*60*24*this.expires)
103 cookietext += "; expires=" + expiredate.toGMTString()
104 } else { // assume it's a date object
105 cookietext += "; expires=" + this.expires.toGMTString()
106 } // end of typeof(this.expires) if
107 } // end of this.expires != null if
109 // add path, if specified
110 if (this.accessPath != null) {
111 cookietext += "; PATH="+this.accessPath }
114 // alert("writing "+cookietext)
115 document.cookie = cookietext
121 var search = this.name + "="
122 var CookieString = document.cookie
123 if(CookieString == null) CookieString = "";
126 if (CookieString.length > 0) {
127 offset = CookieString.indexOf(search)
129 offset += search.length
130 end = CookieString.indexOf(";", offset)
131 if (end == -1) { // cookie is last item in the string, so no terminator
132 end = CookieString.length }
133 this.rawValue = CookieString.substring(offset, end)
138 if (this.rawValue != null) { // unpack into fields
140 var sl = this.rawValue.length
145 // Special case - single-field cookies written by other functions,
146 // so without a '#' terminator
148 if (this.rawValue.substr(sl-1, 1) != this.fieldSeparator) {
149 this.fields[0] = unescape(this.rawValue)
150 } else { // separate fields
154 endidx = this.rawValue.indexOf(this.fieldSeparator, startidx)
156 this.fields[i] = unescape(this.rawValue.substring(startidx, endidx))
158 startidx = endidx + 1}
160 while (endidx !=-1 & endidx != (this.rawValue.length -1));
162 } // end of unpack into fields if block
167 function ucDelete() {