濮阳杆衣贸易有限公司

主頁 > 知識庫 > HTML5 Web存儲方式的localStorage和sessionStorage進(jìn)行數(shù)據(jù)本地存儲案例應(yīng)用

HTML5 Web存儲方式的localStorage和sessionStorage進(jìn)行數(shù)據(jù)本地存儲案例應(yīng)用

熱門標(biāo)簽:洛陽市伊川縣地圖標(biāo)注中心官網(wǎng) 高德地圖標(biāo)注錯誤怎么修改 地圖標(biāo)注自己去過的地方 平頂山電子地圖標(biāo)注怎么修改 電銷機(jī)器人視頻 江蘇高頻外呼系統(tǒng)線路 搜狗星級酒店地圖標(biāo)注 會聲會影怎樣做地圖標(biāo)注效果 標(biāo)準(zhǔn)智能外呼系統(tǒng)

使用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

 

復(fù)制代碼
代碼如下:

<!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ù)顯示在頁面上。
function saveSession() {
var t1 = document.getElementById("t1");
var t2 = document.getElementById("t2");
var mydata = t2.value;
var oStorage = window.sessionStorage;
oStorage.mydata = mydata;
t1.value += "sessionStorage保存mydata:" + mydata + "\n";
}
function readSession() {
var t1 = document.getElementById("t1");
var oStorage = window.sessionStorage;
var mydata = "不存在";
if (oStorage.mydata) {
mydata = oStorage.mydata;
}
t1.value += "sessionStorage讀取mydata:" + mydata + "\n";
}
function cleanSession() {
var t1 = document.getElementById("t1");
var oStorage = window.sessionStorage;
var mydata = "不存在";
if (oStorage.mydata) {
mydata = oStorage.mydata;
}
oStorage.removeItem("mydata");
t1.value += "sessionStorage清除mydata:" + mydata + "\n";
}
function saveStorage() {
var t1 = document.getElementById("t1");
var t2 = document.getElementById("t2");
var mydata = t2.value;
var oStorage = window.localStorage;
oStorage.mydata = mydata;
t1.value += "localStorage保存mydata:" + mydata + "\n";
}
function readStorage() {
var t1 = document.getElementById("t1");
var oStorage = window.localStorage;
var mydata = "不存在";
if (oStorage.mydata) {
mydata = oStorage.mydata;
}
t1.value += "localStorage讀取mydata:" + mydata + "\n";
}
function cleanStorage() {
var t1 = document.getElementById("t1");
var oStorage = window.localStorage;
var mydata = "不存在";
if (oStorage.mydata) {
mydata = oStorage.mydata;
}
oStorage.removeItem("mydata");
t1.value += "localStorage清除mydata:" + mydata + "\n";
}
</script>
<div>
<textarea id="t1"></textarea>
<label>需要保存的數(shù)據(jù): </label>
<input type="text" id="t2" />
<input type="button" class="button" onclick="saveSession()" name="b1" value="session保存" />
<input type="button" class="button" onclick="readSession()" value="session讀取" />
<input type="button" class="button" onclick="cleanSession()" value="session清除" />
<input type="button" class="button" onclick="saveStorage()" value="local保存" />
<input type="button" class="button" onclick="readStorage()" value="local讀取" />
<input type="button" class="button" onclick="cleanStorage()" value="local清除" />
</div>
</body>
</html>

標(biāo)簽:廣東 常德 阿克蘇 鄂爾多斯 蚌埠 廣西 松原 果洛

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《HTML5 Web存儲方式的localStorage和sessionStorage進(jìn)行數(shù)據(jù)本地存儲案例應(yīng)用》,本文關(guān)鍵詞  HTML5,Web,存儲,方式,的,localStorage,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《HTML5 Web存儲方式的localStorage和sessionStorage進(jìn)行數(shù)據(jù)本地存儲案例應(yīng)用》相關(guān)的同類信息!
  • 本頁收集關(guān)于HTML5 Web存儲方式的localStorage和sessionStorage進(jìn)行數(shù)據(jù)本地存儲案例應(yīng)用的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    顺平县| 友谊县| 泰顺县| 大理市| 高雄县| 自治县| 南通市| 咸阳市| 隆安县| 北票市| 新兴县| 盖州市| 巴青县| 卓资县| 溆浦县| 武安市| 惠安县| 玛沁县| 大名县| 裕民县| 平武县| 蓬溪县| 洪雅县| 延津县| 阿拉善右旗| 唐河县| 蕲春县| 邹城市| 元氏县| 略阳县| 江永县| 涡阳县| 韩城市| 思茅市| 巴东县| 清苑县| 石门县| 新平| 台东市| 安吉县| 宁国市|