快速理解webStroage

來源:互聯網
上載者:User

標籤:

<!DOCTYPE html><html>    <head>        <meta charset="utf-8" />        <title></title>    </head>    <body>        <fieldset id="">            <legend>sessionStorage</legend>            <input type="text" name="" id="txt1" value="" />            <br />            <input type="button" name="save1" id="save1" value="儲存資料" />            <input type="button" name="get1" id="get1" value="讀取資料" />            <input type="button" name="del1" id="del1" value="刪除資料" />        </fieldset>        <fieldset id="">            <legend>localStorage</legend>            <input type="text" name="" id="txt2" value="" />            <br />            <input type="button" name="save2" id="save2" value="儲存資料" />            <input type="button" name="get2" id="get2" value="讀取資料" />            <input type="button" name="del2" id="del2" value="刪除資料" />        </fieldset>    </body>    <script type="text/javascript">                document.getElementById("save1").onclick = function(){            var sinput = document.getElementById("txt1").value;            sessionStorage.setItem("session1",sinput);        }        document.getElementById("get1").onclick = function(){            var con = sessionStorage.getItem("session1");            var str = document.createElement("span");            str.innerHTML = con;            document.getElementsByTagName("fieldset")[0].appendChild(str);        }        document.getElementById("del1").onclick = function(){            sessionStorage.removeItem("session1");            document.getElementsByTagName("fieldset")[0].getElementsByTagName("span")[0].innerHTML=""        }                document.getElementById("save2").onclick = function(){            var sinput = document.getElementById("txt2").value;            localStorage.setItem("local1",sinput);        }        document.getElementById("get2").onclick = function(){            var con = localStorage.getItem("local1");            var str = document.createElement("span");            str.innerHTML = con;            document.getElementsByTagName("fieldset")[1].appendChild(str);        }        document.getElementById("del2").onclick = function(){            localStorage.removeItem("local1");            document.getElementsByTagName("fieldset")[1].getElementsByTagName("span")[0].innerHTML=""        }            </script></html>

Web Storage功能,就是在Web上儲存資料,分為兩種:

sessionStorage:將資料儲存在session對象中。所謂session,是指使用者在瀏覽某個網站時,從進入網站到該網站所有網域名稱被關閉所經過的這段時間,session對象可用來儲存在這段時間內所要求儲存的任何資料


localStorage:將資料儲存在用戶端本地的硬體裝置中,即使瀏覽器關閉了,該資料仍然存在,下次開啟瀏覽器訪問網站時仍然可用

區別:sessionStorage為臨時儲存,localStorage為永久儲存。

HTML5中提供了localStorage對象可以將資料長期儲存在用戶端,直到人為清除。localStorage提供了幾個方法:
1、儲存:localStorage.setItem(key,value)
如果key存在時,更新value

2、擷取:localStorage.getItem(key)
如果key不存在返回null

3、刪除:localStorage.removeItem(key)
一旦刪除,key對應的資料將會全部刪除

4、全部清除:localStorage.clear()
某些時候使用removeItem逐個刪除太麻煩,可以使用clear,執行的後果是會清除所有localStorage對象儲存的資料

5、遍曆localStorage儲存的key
.length 資料總量,例:localStorage.length
.key(index) 擷取key,例:var key=localStorage.key(index);

6、儲存JSON格式資料
JSON.stringify(data)  將一個對象轉換成JSON格式的資料串,返迴轉換後的串
JSON.parse(data) 將資料解析成對象,返回解析後的對象。

sessionStroage的用法和localStroage的用法一樣,在儲存資料的過程中,所有資料全部以字串類型儲存。

存取對象時用Json.stringfy()方法將對象轉換成字串

var str = {            age:12,            name:"sa"        }        str=JSON.stringify(str);        localStorage.setItem("obj",str);

 將字串轉換成對象用JSON.parse()方法

    console.log(JSON.parse(localStorage.getItem("obj")))

 

快速理解webStroage

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.