HTML5中的storage存储

来源:互联网 发布:房天下 源码 编辑:程序博客网 时间:2024/04/30 14:49

1.个人标示

caicongyang:http://blog.csdn.net/caicongyang

2.代码:

<!DOCTYPE html><html>  <head>    <title>storageHtml.html</title>    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    <meta http-equiv="description" content="this is my page">    <meta http-equiv="content-type" content="text/html; charset=UTF-8">        <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->  </head>    <body>    This is my storageHtml page. <br>  </body>  <script type="text/javascript">  if(window.localStorage){  alert("您的浏览器支持本地存储!");  //支持键值对的形式-设置键值对  localStorage.setItem("name","张三");  //键值对长度  alert(localStorage.length);  //获取键值对的值  alert(localStorage.getItem("name"));  //通过索引获取键值对的值  alert(localStorage.key(0));  //移除某个键值对  localStorage.removeItem("name");  //清除所有的键值对  localStorage.clear();  alert(localStorage.length);      //localStorage同样支持josn格式的数据对象  var user = {  name:"李四",  age:"15"  };    localStorage.setItem("u1",JSON.stringify(user));  var u = JSON.parse(localStorage.getItem("u1"));  alert(u.name);      }  if(window.sessionStorage){  alert("您的浏览器支持会话存储!");  //sessionStorage的方法与localStorage相同  }    //本地存储数据监听    window.onload = function(){  window.addEventListener("storage",function(e){  console.log(e);  },true);  };  </script></html>

3.我的博客

http://blog.csdn.net/caicongyang



1 0