HTML5 Web存儲(chǔ)方式的localStorage和sessionStorage進(jìn)行數(shù)據(jù)本地存儲(chǔ)案例應(yī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ù),沒有時(shí)間限制,一天,一年,兩年甚至更長(zhǎng),數(shù)據(jù)都可以使用。
sessionStorage(會(huì)話存儲(chǔ)),只有在瀏覽器被關(guān)閉之前使用,創(chuàng)建另一個(gè)頁(yè)面時(shí)同意可以使用,關(guān)閉瀏覽器之后數(shù)據(jù)就會(huì)消失。
某個(gè)博主的測(cè)試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存儲(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>
相關(guān)文章

html5超簡(jiǎn)單的localStorage實(shí)現(xiàn)記住密碼的功能實(shí)現(xiàn)
這篇文章主要介紹了html5超簡(jiǎn)單的localStorage實(shí)現(xiàn)記住密碼的功能實(shí)現(xiàn),非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-09-07HTML5 LocalStorage 本地存儲(chǔ)詳細(xì)概括(多圖)
這篇文章主要介紹了HTML5 LocalStorage 本地存儲(chǔ),給標(biāo)簽元素添加屬性和瀏覽器兼容性都做了詳細(xì)概括,具體操作步驟大家可查看下文的詳細(xì)講解,感興趣的小伙伴們可以參考一2017-08-18html5 localStorage本地存儲(chǔ)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
localStorage 即本地存儲(chǔ),可用于長(zhǎng)久保存整個(gè)網(wǎng)站的數(shù)據(jù),保存的數(shù)據(jù)沒有過期時(shí)間,直到手動(dòng)去除2017-07-06html5中l(wèi)ocalStorage本地存儲(chǔ)的簡(jiǎn)單使用
我們?cè)谧鲰?yè)面時(shí)會(huì)用到本地存儲(chǔ)的時(shí)候,今天說說localStorage本地存儲(chǔ)。感興趣的朋友一起學(xué)習(xí)吧2017-06-16
localstorage在瀏覽器的API有兩個(gè):localStorage和sessionStorage,存在于window對(duì)象中:localStorage對(duì)應(yīng)window.localStorage,sessionStorage對(duì)應(yīng)window.sessionStorage2017-05-09HTML5 LocalStorage 本地存儲(chǔ)刷新值還在
html5的兩種存儲(chǔ)技術(shù)的最大區(qū)別就是生命周期,接下來通過本文給大家分享HTML5 LocalStorage 本地存儲(chǔ)刷新值還在問題以及使用方法小結(jié),需要的的朋友參考下本文吧2017-03-10- 在HTML5中,新加入了一個(gè)localStorage特性,這個(gè)特性主要是用來作為本地存儲(chǔ)來使用的,解決了cookie存儲(chǔ)空間不足的問題(cookie中每條cookie的存儲(chǔ)空間為4k),localStorage2017-02-22
Html5中l(wèi)ocalStorage存儲(chǔ)JSON數(shù)據(jù)并讀取JSON數(shù)據(jù)的實(shí)現(xiàn)方法
localStorage是HTML5提供的再客戶端實(shí)現(xiàn)本地存儲(chǔ)的一種方法,但是localStorage方法只能存儲(chǔ)字符串?dāng)?shù)據(jù),有時(shí)候我們需要存儲(chǔ)對(duì)象到本地比如:JSON;那么,localStorage怎么2017-02-13詳解HTML5 LocalStorage 本地存儲(chǔ)
本篇文章主要介紹了HTML5 LocalStorage 本地存儲(chǔ) ,HTML5 storage提供了一種方式讓網(wǎng)站能夠把信息存儲(chǔ)到你本地的計(jì)算機(jī)上,并再以后需要的時(shí)候進(jìn)行獲取。有興趣的可以了解2016-12-23html5本地存儲(chǔ) localStorage操作使用詳解
這篇文章主要介紹了html5本地存儲(chǔ) localStorage操作使用詳解的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-20



