HTML5 Web Storage

付费节点推荐


免费节点


节点使用教程


时间:2015年3月29日

地点:甲由传媒(实习)

20160329102904

20160329102921

sessionStorage Demo

数据存储在session,关闭浏览器或等到session timeout都可以清除数据
 





html5 web storage








function saveStorage(id){
var target = document.getElementById(id);
var str= target.value;
sessionStorage.setItem("message",str);

}

function loadStorage(id){
var target =document.getElementById(id);
var msg = sessionStorage.getItem("message");
target.innerHTML = msg;

}

lcoalStorage Demo

localStorage里存储的数据会保存在本地,需清除cookies才能完全删除。

function saveStorage(id){
var target = document.getElementById(id);
var str = target.value;
localStorage.setItem("message",str);

}

function loadStorage(id){
var target = document.getElementById(id);
var msg = localStorage.getItem("message");
target.innerHTML = msg;

}

web Storage制作留言Demo






web Storage留言







function saveStorage(id){
var data = document.getElementById(id).value;
var time = new Date().getTime();
localStorage.setItem(time,data);
alert("数据以存储");
loadStorage('msg');
}

function loadStorage(id){
var result = "

";
for(var i=0; i

";

}
result +="

"+date+"

";
var target =document.getElementById(id);
target.innerHTML = result;

}

function clearStorage(){
localStorage.clear();
alert("数据已删除");
loadStorage('msg');

}

localStorage 简单数据库






localStorage 简单数据库


姓名:
Email:
电话号码:
备注:

检索:


function saveStorage(){
var data = new Object;
data.name=document.getElementById("name").value;
data.email=document.getElementById("email").value;
data.tel=document.getElementById("tel").value;
data.memo=document.getElementById("memo").value;
var str = JSON.stringify(data); //Object,数组 -> json str,可参考此文档
localStorage.setItem(data.name,str);
alert("数据已保存");

}

function findStorage(id){
var find = document.getElementById('find').value;
var str = localStorage.getItem(find);
var data = JSON.parse(str); //将json str -> Obj,可参考此文档
var result ="姓名:"+data.name +"
";
result +="Email:"+data.email +"
";
result +="电话:"+data.tel +"
";
result +="备注:"+data.memo +"
";

var target = document.getElementById(id);
target.innerHTML = result;

}

运行结果:
0160329152046

未经允许不得转载:Bcoder资源网 » HTML5 Web Storage

相关推荐

赞 (1)
分享到:更多 ()

评论 0

评论前必须登录!

登陆 注册
  1. Coder.Binghttp://bcoder.clbug.com/5017.html JSON.stringify()文档 JSON.parse()