一、浏览器支持
检测方法:
if(window.localStorage){
alert('YES');
}else{
alert('NO');
}
二、操作
写入
localStorage.testkey = testval;
localStorage["testkey"] = "testval2";
localStorage.setItem("testkey2","testval");
读取
var a1 = localStorage["testkey"];
var a2 = localStorage.testkey;
var b = localStorage.getItem("testkey2");
读取本站所有本地存储
for(var i=0;i<storage.length;i++){ //循环显示localStorage里的键值对
//key(i)获得相应的键,再用getItem()方法获得对应的值
document.write(storage.key(i)+ " : " + storage.getItem(storage.key(i)) + "<br>");
}
删除
localStorage.removeItem("testkey");