localStorage
html5标准 Web 存储
现在的主流浏览器,包括IE 8+、Chrome 4+、Firefox 3.5+、Opera 10.5+、Safari 4+、iPhone 2+、Android 2+等,都支持localStorage本地存储功能。localStorage没有时间上的限制,只要不主动删除,会一直保存。
单条value没有长度限制,但是每个站点使用总量有限制。chrome的上限是4.9MB,其它浏览器的容量上限也大都在2.5-10MB之间。
检测浏览器是否支持localStorage
function check_localStorage_support(){ if(window.localStorage){ return true; } alert("Sorry,your browser does NOT support localStorage!"); return false;}
localStorage 基于键值对存储,读、写、删操作很简单
localStorage.a = 3; //设置a为"3"localStorage["a"] = "abc"; //设置a为"abc",也覆盖了上面的值var a1 = localStorage["a"]; //获取a的值var a2 = localStorage.a; //获取a的值console.log(a1 + "\t" + a2);localStorage.setItem("b","I am b"); //设置b为"I am b"var b = localStorage.getItem("b"); //获取b的值console.log(b);localStorage.removeItem("b"); //清除b的值console.log(localStorage.getItem("b"));//输出所有键值对function showStorage(){ var storage = window.localStorage; for(var i=0;i