您好,登錄后才能下訂單哦!
使用HTML5 Web存儲的localStorage和sessionStorage方式進(jìn)行Web頁面數(shù)據(jù)本地存儲。
頁面參考如下圖,能將頁面上的數(shù)據(jù)進(jìn)行本地存儲。并能讀取存儲的數(shù)據(jù)顯示在頁面上。
localStorage(本地存儲),可以長期存儲數(shù)據(jù),沒有時間限制,一天,一年,兩年甚至更長,數(shù)據(jù)都可以使用。
sessionStorage(會話存儲),只有在瀏覽器被關(guān)閉之前使用,創(chuàng)建另一個頁面時同意可以使用,關(guān)閉瀏覽器之后數(shù)據(jù)就會消失。
某個博主的測試localStorage兼容情況,如下:
Chrome4+ 開始支持localStorage
Firefox3.5+開始支持localStorage
Firefox1.5+支持globalStorage
IE8+支持localStorage
IE7兼容模式支持localStorage
IE5.5+支持userdata
Safari 4+ 支持localStorage
Opera10.5+支持localStorage
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <style type="text/css"> textarea { width: 300px; height: 300px; } .button { width: 100px; } </style> </head> <body <script type="text/javascript"> //使用HTML5 Web存儲的localStorage和sessionStorage方式進(jìn)行Web頁面數(shù)據(jù)本地存儲。 //頁面參考如下圖,能將頁面上的數(shù)據(jù)進(jìn)行本地存儲。并能讀取存儲的數(shù)據(jù)顯示在頁面上。 var t1; var t2; var oStorage; var oStorage; function init() {//初始化t1,t2 t1 = document.getElementById("t1"); t2 = document.getElementById("t2"); sStorage = window.sessionStorage; lStorage = window.localStorage } function saveSession() { sStorage.mydata = t2.value; t1.value += "sessionStorage保存mydata:" + t2.value + "\n"; } function readSession() { t1.value += "sessionStorage讀取mydata:" + sStorage.mydata + "\n"; } function cleanSession() { t1.value += "sessionStorage清除mydata:" + sStorage.mydata + "\n"; sStorage.removeItem("mydata"); } function saveStorage() { lStorage.mydata = t2.value; t1.value += "localStorage保存mydata:" + t2.value + "\n"; } function readStorage() { t1.value += "localStorage讀取mydata:" + lStorage.mydata + "\n"; } function cleanStorage() { t1.value += "localStorage清除mydata:" + lStorage.mydata + "\n"; lStorage.removeItem("mydata"); } </script> <div> <textarea id="t1"></textarea><br /> <label>需要保存的數(shù)據(jù): </label> <input type="text" id="t2" /><br /> <input type="button" class="button" name="b1" value="session保存" /> <input type="button" class="button" value="session讀取" /> <input type="button" class="button" value="session清除" /><br /> <input type="button" class="button" value="local保存" /> <input type="button" class="button" value="local讀取" /> <input type="button" class="button" value="local清除" /> </div> </body> </html>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。