2 DISCLAIMER: THESE JAVASCRIPT FUNCTIONS ARE SUPPLIED 'AS IS', WITH
3 NO WARRANTY EXPRESSED OR IMPLIED. YOU USE THEM AT YOUR OWN RISK.
4 PAUL STEPHENS DOES NOT ACCEPT ANY LIABILITY FOR
5 ANY LOSS OR DAMAGE RESULTING FROM THEIR USE, HOWEVER CAUSED.
7 Paul Stephens' cookie-handling object library
10 2.0 - Introduces field names
11 2.1 - Fixes bug where undefined embedded fields[] elements weren't written to disk
15 TO USE THIS LIBRARY, INSERT ITS CONTENTS IN THE <HEAD> SECTION
16 OF YOUR WEB PAGE SOURCE, BEFORE ANY OTHER JAVASCRIPT ROUTINES.
18 (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.
21 function cookieObject(name, expires, accessPath) {
24 this.fieldSeparator = "#"
26 this.expires = expires
27 this.accessPath = accessPath
29 this.fields = new Array()
30 this.fieldnames = new Array()
31 if (arguments.length > 3) {
33 for (i = 3; i < arguments.length; i++) {
34 this.fieldnames[j] = arguments[i]
37 this.fields.length = this.fieldnames.length
41 this.remove = ucDelete
44 this.namepos = ucNamePos
48 function ucFieldGet(fieldname) {
49 var i = this.namepos(fieldname)
53 return "BadFieldName!"
56 function ucFieldPut (fieldname, fieldval) {
57 var i = this.namepos(fieldname)
59 i = this.fieldnames.length;
60 this.fieldnames[i] = fieldname;
62 this.fields[i] = fieldval
65 function ucNamePos(fieldname) {
67 for (i = 0; i < this.fieldnames.length; i++) {
68 if (fieldname == this.fieldnames[i]) {
75 var cookietext = this.name + "="
76 if (this.fields.length == 1) {
77 cookietext += escape(this.fields[0])
79 for (i= 0; i < this.fields.length; i++) {
80 cookietext += escape(this.fields[i]) + this.fieldSeparator }
82 if (this.expires != null) {
83 if (typeof(this.expires) == "number") {
85 var expiredate = new Date()
86 expiredate.setTime(today.getTime() + 1000*60*60*24*this.expires)
87 cookietext += "; expires=" + expiredate.toGMTString()
89 cookietext += "; expires=" + this.expires.toGMTString()
92 if (this.accessPath != null) {
93 cookietext += "; PATH="+this.accessPath }
94 document.cookie = cookietext
98 var search = this.name + "="
99 var CookieString = document.cookie
100 if(CookieString == null) CookieString = "";
103 if (CookieString.length > 0) {
104 offset = CookieString.indexOf(search)
106 offset += search.length
107 end = CookieString.indexOf(";", offset)
109 end = CookieString.length }
110 this.rawValue = CookieString.substring(offset, end)
114 if (this.rawValue != null) { // unpack into fields
115 var sl = this.rawValue.length
119 if (this.rawValue.substr(sl-1, 1) != this.fieldSeparator) {
120 this.fields[0] = unescape(this.rawValue)
124 endidx = this.rawValue.indexOf(this.fieldSeparator, startidx)
126 this.fields[i] = unescape(this.rawValue.substring(startidx, endidx))
128 startidx = endidx + 1}
130 while (endidx !=-1 & endidx != (this.rawValue.length -1));
135 function ucDelete() {