1// The constructor function: creates a cookie object for the specified 2// document, with a specified name and optional attributes. 3// Arguments: 4// document: The Document object that the cookie is stored for. Required. 5// name: A string that specifies a name for the cookie. Required. 6// hours: An optional number that specifies the number of hours from now 7// that the cookie should expire. 8// path: An optional string that specifies the cookie path attribute. 9// domain: An optional string that specifies the cookie domain attribute. 10// secure: An optional Boolean value that, if true, requests a secure cookie. 11// 12function Cookie(document, name, hours, path, domain, secure) 13{ 14 // All the predefined properties of this object begin with '$' 15 // to distinguish them from other properties which are the values to 16 // be stored in the cookie. 17 this.$document = document; 18 this.$name = name; 19 if (hours) 20 this.$expiration = new Date((new Date()).getTime() + hours*3600000); 21 else this.$expiration = null; 22 if (path) this.$path = path; else this.$path = null; 23 if (domain) this.$domain = domain; else this.$domain = null; 24 if (secure) this.$secure = true; else this.$secure = false; 25} 26 27// This function is the store() method of the Cookie object. 28function _Cookie_store() 29{ 30 // First, loop through the properties of the Cookie object and 31 // put together the value of the cookie. Since cookies use the 32 // equals sign and semicolons as separators, we'll use colons 33 // and ampersands for the individual state variables we store 34 // within a single cookie value. Note that we escape the value 35 // of each state variable, in case it contains punctuation or other 36 // illegal characters. 37 var cookieval = ""; 38 for(var prop in this) { 39 // Ignore properties with names that begin with '$' and also methods. 40 if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function')) 41 continue; 42 if (cookieval != "") cookieval += '&'; 43 cookieval += prop + ':' + escape(this[prop]); 44 } 45 46 // Now that we have the value of the cookie, put together the 47 // complete cookie string, which includes the name and the various 48 // attributes specified when the Cookie object was created. 49 var cookie = this.$name + '=' + cookieval; 50 if (this.$expiration) 51 cookie += '; expires=' + this.$expiration.toGMTString(); 52 if (this.$path) cookie += '; path=' + this.$path; 53 if (this.$domain) cookie += '; domain=' + this.$domain; 54 if (this.$secure) cookie += '; secure'; 55 56 // Now store the cookie by setting the magic Document.cookie property. 57 this.$document.cookie = cookie; 58} 59// This function is the load() method of the Cookie object. 60function _Cookie_load() 61{ 62 // First, get a list of all cookies that pertain to this document. 63 // We do this by reading the magic Document.cookie property. 64 var allcookies = this.$document.cookie; 65 if (allcookies == "") return false; 66 67 // Now extract just the named cookie from that list. 68 var start = allcookies.indexOf(this.$name + '='); 69 if (start == -1) return false; // Cookie not defined for this page. 70 start += this.$name.length + 1; // Skip name and equals sign. 71 var end = allcookies.indexOf(';', start); 72 if (end == -1) end = allcookies.length; 73 var cookieval = allcookies.substring(start, end); 74 75 // Now that we've extracted the value of the named cookie, we've 76 // got to break that value down into individual state variable 77 // names and values. The name/value pairs are separated from each 78 // other by ampersands, and the individual names and values are 79 // separated from each other by colons. We use the split method 80 // to parse everything. 81 var a = cookieval.split('&'); // Break it into array of name/value pairs. 82 for(var i=0; i < a.length; i++) // Break each pair into an array. 83 a[i] = a[i].split(':'); 84 85 // Now that we've parsed the cookie value, set all the names and values
86 // of the state variables in this Cookie object. Note that we unescape() 87 // the property value, because we called escape() when we stored it. 88 for(var i = 0; i < a.length; i++) { 89 this[a[i][0]] = unescape(a[i][1]); 90 } 91 92 // We're done, so return the success code. 93 return true; 94} 95 96// This function is the remove() method of the Cookie object. 97function _Cookie_remove() 98{ 99 var cookie; 100 cookie = this.$name + '='; 101 if (this.$path) cookie += '; path=' + this.$path; 102 if (this.$domain) cookie += '; domain=' + this.$domain; 103 cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT'; 104 105 this.$document.cookie = cookie; 106} 107 108// Create a dummy Cookie object, so we can use the prototype object to make 109// the functions above into methods. 110new Cookie(); 111Cookie.prototype.store = _Cookie_store; 112Cookie.prototype.load = _Cookie_load; 113Cookie.prototype.remove = _Cookie_remove; 114 115
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.