cookies.js
| 1 |
// name = string equal to the name of the instance of the object
|
|---|---|
| 2 |
// defaultExpiration = number of units to make the default expiration date for the cookie
|
| 3 |
// expirationUnits = 'seconds' | 'minutes' | 'hours' | 'days' | 'months' | 'years' (default is 'days')
|
| 4 |
// defaultDomain = string, default domain for cookies; default is current domain minus the server name
|
| 5 |
// defaultPath = string, default path for cookies; default is '/'
|
| 6 |
function Cookiemanager(name,defaultExpiration,expirationUnits,defaultDomain,defaultPath) { |
| 7 |
// remember our name
|
| 8 |
this.name = name;
|
| 9 |
// get the default expiration
|
| 10 |
this.defaultExpiration = this.getExpiration(defaultExpiration,expirationUnits); |
| 11 |
// set the default domain to defaultDomain if supplied; if not, set it to document.domain
|
| 12 |
// if document.domain is numeric, otherwise strip off the server name and use the remainder
|
| 13 |
this.defaultDomain = (defaultDomain)?defaultDomain:(document.domain.search(/[a-zA-Z]/) == -1)?document.domain:document.domain.substring(document.domain.indexOf('.') + 1,document.domain.length); |
| 14 |
// set the default path
|
| 15 |
this.defaultPath = (defaultPath)?defaultPath:'/'; |
| 16 |
// initialize an object to hold all the document's cookies
|
| 17 |
this.cookies = new Object(); |
| 18 |
// initialize an object to hold expiration dates for the doucment's cookies
|
| 19 |
this.expiration = new Object(); |
| 20 |
// initialize an object to hold domains for the doucment's cookies
|
| 21 |
this.domain = new Object(); |
| 22 |
// initialize an object to hold paths for the doucment's cookies
|
| 23 |
this.path = new Object(); |
| 24 |
// set an onlunload function to write the cookies
|
| 25 |
window.onunload = new Function (this.name+'.setDocumentCookies();'); |
| 26 |
// get the document's cookies
|
| 27 |
this.getDocumentCookies();
|
| 28 |
} |
| 29 |
// gets an expiration date for a cookie as a GMT string
|
| 30 |
// expiration = integer expressing time in units (default is 7 days)
|
| 31 |
// units = 'miliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'months' | 'years' (default is 'days')
|
| 32 |
Cookiemanager.prototype.getExpiration = function(expiration,units) { |
| 33 |
// set default expiration time if it wasn't supplied
|
| 34 |
expiration = (expiration)?expiration:7;
|
| 35 |
// supply default units if units weren't supplied
|
| 36 |
units = (units)?units:'days';
|
| 37 |
// new date object we'll use to get the expiration time
|
| 38 |
var date = new Date(); |
| 39 |
// set expiration time according to units supplied
|
| 40 |
switch(units) {
|
| 41 |
case 'years': |
| 42 |
date.setFullYear(date.getFullYear() + expiration); |
| 43 |
break;
|
| 44 |
case 'months': |
| 45 |
date.setMonth(date.getMonth() + expiration); |
| 46 |
break;
|
| 47 |
case 'days': |
| 48 |
date.setTime(date.getTime()+(expiration*24*60*60*1000)); |
| 49 |
break;
|
| 50 |
case 'hours': |
| 51 |
date.setTime(date.getTime()+(expiration*60*60*1000)); |
| 52 |
break;
|
| 53 |
case 'minutes': |
| 54 |
date.setTime(date.getTime()+(expiration*60*1000)); |
| 55 |
break;
|
| 56 |
case 'seconds': |
| 57 |
date.setTime(date.getTime()+(expiration*1000));
|
| 58 |
break;
|
| 59 |
default:
|
| 60 |
date.setTime(date.getTime()+expiration); |
| 61 |
break;
|
| 62 |
} |
| 63 |
// return expiration as GMT string
|
| 64 |
return date.toGMTString();
|
| 65 |
} |
| 66 |
// gets all document cookies and populates the .cookies property with them
|
| 67 |
Cookiemanager.prototype.getDocumentCookies = function() { |
| 68 |
var cookie,pair;
|
| 69 |
// read the document's cookies into an array
|
| 70 |
var cookies = document.cookie.split(';'); |
| 71 |
// walk through each array element and extract the name and value into the cookies property
|
| 72 |
var len = cookies.length;
|
| 73 |
for(var i=0;i < len;i++) { |
| 74 |
cookie = cookies[i]; |
| 75 |
// strip leading whitespace
|
| 76 |
while (cookie.charAt(0)==' ') cookie = cookie.substring(1,cookie.length); |
| 77 |
// split name/value pair into an array
|
| 78 |
pair = cookie.split('=');
|
| 79 |
// use the cookie name as the property name and value as the value
|
| 80 |
|
| 81 |
// don't touch any cookies except the one for the fontsizer!!
|
| 82 |
if (pair[0] == 'efaSize') { |
| 83 |
this.cookies[pair[0]] = pair[1]; |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
// sets all document cookies
|
| 88 |
Cookiemanager.prototype.setDocumentCookies = function() { |
| 89 |
var expires = ''; |
| 90 |
var cookies = ''; |
| 91 |
var domain = ''; |
| 92 |
var path = ''; |
| 93 |
for(var name in this.cookies) { |
| 94 |
// see if there's a custom expiration for this cookie; if not use default
|
| 95 |
expires = (this.expiration[name])?this.expiration[name]:this.defaultExpiration; |
| 96 |
// see if there's a custom path for this cookie; if not use default
|
| 97 |
path = (this.path[name])?this.path[name]:this.defaultPath; |
| 98 |
// see if there's a custom domain for this cookie; if not use default
|
| 99 |
domain = (this.domain[name])?this.domain[name]:this.defaultDomain; |
| 100 |
// add to cookie string
|
| 101 |
cookies = name + '=' + this.cookies[name] + '; expires=' + expires + '; path=' + path + '; domain=' + domain; |
| 102 |
document.cookie = cookies; |
| 103 |
} |
| 104 |
return true; |
| 105 |
} |
| 106 |
// gets cookie value
|
| 107 |
// cookieName = string, cookie name
|
| 108 |
Cookiemanager.prototype.getCookie = function(cookieName) { |
| 109 |
var cookie = this.cookies[cookieName] |
| 110 |
return (cookie)?cookie:false; |
| 111 |
} |
| 112 |
// stores cookie value, expiration, domain and path
|
| 113 |
// cookieName = string, cookie name
|
| 114 |
// cookieValue = string, cookie value
|
| 115 |
// expiration = number of units in which the cookie should expire
|
| 116 |
// expirationUnits = 'miliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'months' | 'years' (default is 'days')
|
| 117 |
// domain = string, domain for cookie
|
| 118 |
// path = string, path for cookie
|
| 119 |
Cookiemanager.prototype.setCookie = function(cookieName,cookieValue,expiration,expirationUnits,domain,path) { |
| 120 |
this.cookies[cookieName] = cookieValue;
|
| 121 |
// set the expiration if it was supplied
|
| 122 |
if (expiration) this.expiration[cookieName] = this.getExpiration(expiration,expirationUnits); |
| 123 |
// set path if it was supplied
|
| 124 |
if (domain) this.domain[cookieName] = domain; |
| 125 |
if (path) this.path[cookieName] = path; |
| 126 |
return true; |
| 127 |
} |
| 128 |
|
| 129 |
var cookieManager = new Cookiemanager('cookieManager',1,'days'); |