佳木斯湛栽影视文化发展公司

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

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

熱門(mén)標(biāo)簽:智能手機(jī) 檢查注冊(cè)表項(xiàng) 硅谷的囚徒呼叫中心 百度競(jìng)價(jià)點(diǎn)擊價(jià)格的計(jì)算公式 美圖手機(jī) 使用U盤(pán)裝系統(tǒng) 阿里云 網(wǎng)站建設(shè)

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

 

復(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存儲(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>

標(biāo)簽:黃山 懷化 煙臺(tái) 賀州 湘潭 通遼 湖北 山南

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《HTML5 Web存儲(chǔ)方式的localStorage和sessionStorage進(jìn)行數(shù)據(jù)本地存儲(chǔ)案例應(yīng)用》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    凌云县| 泽库县| 新安县| 金昌市| 泊头市| 玉田县| 鄂温| 淳化县| 炉霍县| 自治县| 临清市| 宁河县| 湖北省| 拜城县| 宁夏| 蓬莱市| 奈曼旗| 桃源县| 隆德县| 嫩江县| 乌海市| 靖江市| 普陀区| 黄陵县| 吐鲁番市| 海安县| 太仆寺旗| 包头市| 兴国县| 灵璧县| 富锦市| 磐石市| 庄浪县| 南昌县| 铜川市| 龙海市| 青海省| 新巴尔虎右旗| 鸡东县| 雷山县| 镇原县|