improved cookie functions

This commit is contained in:
Steffen Vogel 2010-10-19 18:57:08 +02:00
parent 60a8938019
commit 1359050efd

View file

@ -58,7 +58,11 @@ $.extend( {
*/
$.extend({
setCookie: function(name, value, options) {
options = options || {};
// defaults
options = options || {
expires: null,
};
if (value === null) {
value = '';
@ -66,10 +70,9 @@ $.extend({
}
var expires = '';
if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
var date;
if (typeof options.expires == 'number') {
if (typeof options.expires == 'number') { // expires x seconds in the future
date = new Date();
date.setTime(date.getTime()
+ (options.expires * 24 * 60 * 60 * 1000));
@ -89,22 +92,15 @@ $.extend({
document.cookie = name + '=' + encodeURIComponent(value) + expires + path + domain + secure;
},
getCookie: function(name) {
var value = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = $.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
value = decodeURIComponent(cookie.substring(name.length + 1));
break;
return decodeURIComponent(cookie.substring(name.length + 1));
}
}
}
return value;
}
});
});