处理大型Cookie或使用本地存储 - jquery和jquery cookie插件 [英] handling large cookies or using local storage - jquery and jquery cookie plugin
问题描述
我有工作代码设置和清除cookie(记住div的风格)点击:
I have the working code which sets and clears cookie (remembers div's style) on click:
var originalAttributes = $('.aaa').attr('style');
$('.aaa').each(function(){
var d = $(this),
id = d.attr('id'),
storedStyle = $.cookie('aaaStyle' + id);
if (storedStyle != undefined){ //style stored
d.attr('style', storedStyle);
}
});
//mouse event functions for class="aaa"
$('#save').click(function () {
$('.aaa').each(function(){
var d = $(this),
id = d.attr('id'),
style = d.attr('style');
if (style != originalAttributes){ //style changed
$.cookie('aaaStyle' + id, style, { expires: 30 });
}
});
});
$('#clear').click(function () {
// unset changes
$('.aaa').attr('style',originalAttributes).each(function(){
var d = $(this),
id = d.attr('id');
$.cookie('aaaStyle' + id, null);
});
});
只有在发生问题时必须处理相同类的很多div - cookie大小可以达到500kb或更多。浏览器每个Cookie只支持4kb。
Only problem which occurs with this is when I have to handle a lot of divs of the same class - cookie size can get to 500kb or more. Browsers supports only 4kb per cookie.
所以问题是 - 如何避免这个问题,使用jquery cookie插件? - gzip或/和分割cookie在足够小的部分?
So the question is - how can this problem be avoided with this function and with the jquery cookie plugin? - gzip or / and splitting the cookie in small enough parts?
(无论如何,为了加快性能(如果可能 - 但如果不是这样的话) )
(in either way, it would be good to have some sort of compression in order to speed up the performance (if possible - but if not, doesn't matter))
编辑:如何使用本地存储实现这种保存 - 清除功能?
edit: how this same "save - clear" functionality can be achieved with the local storage?
edit2:由user2111737解决( http:/ /jsfiddle.net/z8KuE/33 / ) - 使用本地存储而不是Cookie,并且不使用Cookie插件。
edit2: solved by user2111737 (http://jsfiddle.net/z8KuE/33/) - uses local storage instead of cookie and works without cookie plugin.
推荐答案
如果你不需要访问它在服务器端或最终可以手动发送这个数据到服务器与xmlhttprequest我想你应该尝试localStorage,最终sessionStorage而不是cookie,那么你有20mb 200在IE,但与其他网站共享)。关于压缩 - 你可以考虑使用存储的数据在自定义格式和重建html代码 - 例如。我怀疑这个类可以绝对任何东西 - 我想它可以保存为数字 - 或更好 - 一个字符。它给你255个类保存为一个符号
if you don't need to access it on server side or eventually it's possible to manually send this data to server with xmlhttprequest I think you should rather try localStorage, eventually sessionStorage instead of cookies, then you have 20mb (200 in IE but shared with other sites). About compression - you can think about custom format and rebuild html code using stored data in fly - eg. i doubt this class can be absolutely anything - i guess it could be saved as number - or even better - one character. It gives you 255 classes saved as one sign
这篇关于处理大型Cookie或使用本地存储 - jquery和jquery cookie插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!