My application users asked if it were possible for pages that contain a jqGrid to remember the filter, sort order and current page of the grid (because when they click a gri
Hi You can make this easier (no other dependency needed) in Pure Js not a jQuery plugin
var prefs = {
data: {},
load: function () {
var the_cookie = document.cookie.split(';');
if (the_cookie[0]) {
this.data = JSON.parse(unescape(the_cookie[0]));
}
return this.data;
},
save: function (expires, path) {
var d = expires || new Date(2020, 02, 02);
var p = path || '/';
document.cookie = escape( JSON.stringify(this.data) )
+ ';path=' + p
+ ';expires=' + d.toUTCString();
}
}
How to use ?
to_save = { "size": 40, "color": "green", "products":"jeans"};//and any other data/filters you wanna store here
prefs.data = to_save;
prefs.save();//now our json object is saved on the client document cookie
// delete
var date_in_the_past = new Date(2000,02,02);
prefs.save(date_in_the_past);
// read
var what = prefs.load();
// load populates prefs.data and also returns
alert(what.color);
// or ...
alert(prefs.data.color);
PS : All modern browsers support native JSON encoding/decoding (Internet Explorer 8+, Firefox 3.1+, Safari 4+, and Chrome 3+). Basically, JSON.parse(str) read more. PSS : the Object i used there is just optimization and removing the dependency of .toJSONstring ... source
Take a look at these jQuery plugins
USE this don't use the custom funcation above https://github.com/carhartl/jquery-cookie
https://github.com/ScottHamper/Cookies