1
0
Fork 0
mirror of https://github.com/koalyptus/TableFilter.git synced 2024-05-11 10:57:04 +02:00
TableFilter/src/cookie.js
2014-10-26 12:42:34 +11:00

62 lines
1.6 KiB
JavaScript

/**
* Cookie utilities
*/
(function(window, TF){
'use strict';
TF.Cookie = {};
TF.Cookie.write = function(name, value, hours){
var expire = '';
if(hours){
expire = new Date((new Date()).getTime() + hours * 3600000);
expire = '; expires=' + expire.toGMTString();
}
document.cookie = name + '=' + escape(value) + expire;
};
TF.Cookie.read = function(name){
var cookieValue = '',
search = name + '=';
if(document.cookie.length > 0){
var cookie = document.cookie,
offset = cookie.indexOf(search);
if(offset !== -1){
offset += search.length;
var end = cookie.indexOf(';', offset);
if(end === -1){
end = cookie.length;
}
cookieValue = unescape(cookie.substring(offset, end));
}
}
return cookieValue;
};
TF.Cookie.remove = function(name){
this.write(name,'',-1);
};
TF.Cookie.valueToArray = function(name, separator){
if(!separator){
separator = ',';
}
//reads the cookie
var val = this.read(name);
//creates an array with filters' values
var arr = val.split(separator);
return arr;
};
TF.Cookie.getValueByIndex = function(name, index, separator){
if(!separator){
separator = ',';
}
//reads the cookie
var val = this.valueToArray(name, separator);
return val[index];
};
})(this, this.TF);