web storage简述

来源:互联网 发布:微信炸金花源码 编辑:程序博客网 时间:2024/06/01 07:13

web storage可以作为替代cookie来保存数据,有两种方法:

1.session storage:

   会存储数据,但是关闭浏览器之后数据丢失

2.local storage:

      保存数据,并且关闭浏览器后数据仍然存在



//html

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title></title>    <script src="storgeweb.js"></script></head><body>    <p id="answer1">session保存的数据会在这里出现</p>    <input type="text" id="input1">    <input type="button" value="保存数据" onclick="sessionSet('input1')">    <input type="button" value="读取数据" onclick="sessionGet('answer1')">    <p id="answer2">local保存的数据会在这里出现</p>    <input type="text" id="input2">    <input type="button" value="保存数据" onclick="localSet('input2')">    <input type="button" value="读取数据" onclick="localGet('answer2')"></body></html>

//js

/** * Created by Administrator on 2016/2/10. *///sessionStorgefunction sessionSet(id){    var target=document.getElementById(id);    var str=target.value;    sessionStorage.setItem("message1",str);}function sessionGet(id){    var target=document.getElementById(id);    var str=sessionStorage.getItem("message1");    target.innerHTML=str;}//localStorgefunction localSet(id){    var target=document.getElementById(id);    var str=target.value;    localStorage.setItem("message2",str);}function localGet(id){    var target=document.getElementById(id);    var str=localStorage.getItem("message2");    target.innerHTML=str;}


0 0