html5 localstorage能存多少

2024-12-22 21:21:40
推荐回答(2个)
回答1:

每个浏览器对localstorage的支持大小是不一样的,chrome是5M ,IE10是1630K你可以用下面的js匿名函数测试不同浏览器对localstorage的支持大小

(function() {
    if(!window.localStorage) {
        console.log('当前浏览器不支持localStorage!')
    }    
    var test = '0123456789';
    var add = function(num) {
        num += num;
        if(num.length == 10240) {
            test = num;
            return;
        }
        add(num);
    }
    add(test);
    var sum = test;
    var show = setInterval(function(){
        sum += test;
        try {
            window.localStorage.removeItem('test');
            window.localStorage.setItem('test', sum);
            console.log(sum.length / 1024 + 'KB');
        } catch(e) {
            alert(sum.length / 1024 + 'KB超出最大限制');
            clearInterval(show);
        }
    }, 0.1)
})()

IE的测试结果

chrome的

回答2:

 html5 局部存储详解 - https://21xrx.com/full_stack/Html/html5_local_storage.html