A couple of things I forgot to mention:

Originally Posted by
jscheuer1
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
Code:
var date = new Date();
date.setUTCDate(date.getUTCDate() + days);
var expires = "; expires="+date.toGMTString();
The usual behaviour of the toGMTString method isn't (technically) appropriate for cookies as it doesn't produce the correct format. It should be replaced by:
Code:
Date.prototype.toGMTString = (function() {
var days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
return function() {
return [days[this.getUTCDay()], ', ',
this.getUTCDate().pad(2), '-',
months[this.getUTCMonth()], '-',
this.getUTCFullYear().pad(4), ' ',
this.getUTCHours().pad(2), ':',
this.getUTCMinutes().pad(2), ':',
this.getUTCSeconds().pad(2), ' GMT'].join('');
};
})();
var setAt=Date.parse(date.toGMTString())
That's a rather round-about way of getting the time in milliseconds. Either convert the object to a number,
call valueOf, or call getTime.
while (c.charAt(0)==' ') c = c.substring(1,c.length);
I'd define a trim method (or similar):
Code:
String.prototype.ltrim = function() {
return String(this).replace(/^\s+/, '');
};
Alternatively, I'd use a regular expression to search and parse the string:
Code:
function readCookie(name) {
var pattern = new RegExp('(^|;)\\s*' + nameEQ + '\\s*=\\s*([^\\s;]+)', 'g'),
cookie;
return (cookie = pattern.exec(document.cookie))
? cookie[2]
: null;
}
Mike
Bookmarks