获取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) })()

直接在浏览器控制台运行上面的方法。

亲测Chrome浏览器中localStorage最大5120kb,即5M。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM