HTML5 Web存儲(chǔ)方式的localStorage和sessionStorage進(jìn)行數(shù)據(jù)本地存儲(chǔ)案例應(yīng)用
來(lái)源:易賢網(wǎng) 閱讀:1711 次 日期:2016-08-01 16:19:24
溫馨提示:易賢網(wǎng)小編為您整理了“HTML5 Web存儲(chǔ)方式的localStorage和sessionStorage進(jìn)行數(shù)據(jù)本地存儲(chǔ)案例應(yīng)用”,方便廣大網(wǎng)友查閱!

使用HTML5 Web存儲(chǔ)的localStorage和sessionStorage方式進(jìn)行Web頁(yè)面數(shù)據(jù)本地存儲(chǔ)。

頁(yè)面參考如下圖,能將頁(yè)面上的數(shù)據(jù)進(jìn)行本地存儲(chǔ)。并能讀取存儲(chǔ)的數(shù)據(jù)顯示在頁(yè)面上。

localStorage(本地存儲(chǔ)),可以長(zhǎng)期存儲(chǔ)數(shù)據(jù),沒(méi)有時(shí)間限制,一天,一年,兩年甚至更長(zhǎng),數(shù)據(jù)都可以使用。

sessionStorage(會(huì)話存儲(chǔ)),只有在瀏覽器被關(guān)閉之前使用,創(chuàng)建另一個(gè)頁(yè)面時(shí)同意可以使用,關(guān)閉瀏覽器之后數(shù)據(jù)就會(huì)消失。

某個(gè)博主的測(cè)試localStorage兼容情況,如下:

Chrome4+ 開(kāi)始支持localStorage

 

Firefox3.5+開(kāi)始支持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存儲(chǔ)的localStorage和sessionStorage方式進(jìn)行Web頁(yè)面數(shù)據(jù)本地存儲(chǔ)。

//頁(yè)面參考如下圖,能將頁(yè)面上的數(shù)據(jù)進(jìn)行本地存儲(chǔ)。并能讀取存儲(chǔ)的數(shù)據(jù)顯示在頁(yè)面上。

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>

更多信息請(qǐng)查看網(wǎng)頁(yè)制作
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢(xún)回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門(mén)公布的正式信息和咨詢(xún)?yōu)闇?zhǔn)!
相關(guān)閱讀網(wǎng)頁(yè)制作

2025國(guó)考·省考課程試聽(tīng)報(bào)名

  • 報(bào)班類(lèi)型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢(xún) | 簡(jiǎn)要咨詢(xún)須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專(zhuān)用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢(xún)關(guān)注公眾號(hào):hfpxwx
咨詢(xún)QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專(zhuān)用圖標(biāo)