本文介绍了localStorage压缩? - jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个功能,保存并清除div的样式localStorage点击:

I have the function which saves and clears div's style to localStorage on click:

var originalAttributes = $('.aaa').attr('style');
$('.aaa').each(function(){
    var d = $(this),
    id = d.attr('id'),
    storedStyle = window.localStorage.getItem('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 });
            window.localStorage.setItem('aaaStyle' + id, style);
        }
    });

});

$('#clear').click(function () {
    // unset changes
    $('.aaa').attr('style',originalAttributes).each(function(){
        var d = $(this),
        id = d.attr('id');
        window.localStorage.removeItem('aaaStyle' + id);
    });
});

应添加到此代码中,以便本地存储中的数据被压缩?

What should be added to this code so that data in the local storage gets compressed?

(每个浏览器每个网域有内存限制,我想最大化此功能)

(there is memory limit per domain in each browser and I would like to maximize this functionality)

推荐答案

你可以在webworker中压缩字符串,不要在主线程上放置不必要的负载 - 有字符串压缩的库,甚至lzma,据我所知localStorage允许在现代浏览器中分配最多20MB页面和200 mb在所有网站共享的IE - 额外的压缩 - 您可以构建ids和类的映射,如果有少于255个ids和类,并使用String.fromCharCode(int)将它们保存为一个字符并保存为一个大字符串然后在localStorage中解码 - inputstr [pos] .charCodeAt(0)为每对在页面启动时转换为可用对象。然而,这是不好的想法,生成这个大字符串的每一个变化,我会使用onbeforeunload事件在页面关闭之前这样做

you can compress string in webworker to not put unnecessary load on primary thread - there are libraries for strings compression, even lzma, as far as i know localStorage allows to allocate up to 20mb in modern browsers per page and 200 mb in IE shared through all sites - additional compression - you can build map of ids and classes if there is less than 255 ids and classes and save them as one character using String.fromCharCode(int) and save as one big string in localStorage then to decode - inputstr[pos].charCodeAt(0) for every pair to convert to usable object at page startup. However then it's bad idea to generate this big string on every change and i'd use onbeforeunload event to do this before page is closed

这篇关于localStorage压缩? - jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 02:14