html5本地存儲(chǔ)之localstorage 、本地?cái)?shù)據(jù)庫、sessionStorage簡(jiǎn)單使用示例

html5的一個(gè)非常cool的功能,就是web storage,類似于之前的cookie,不過與之不同的是,web storage 擁有本地5兆的容量可以存儲(chǔ),而cookie卻只有4K,這是完全不能比的優(yōu)勢(shì)。
webstrange又分為:localstorage,sessionstorage和本地?cái)?shù)據(jù)庫。
接下來我就來一一介紹:
1、localstorage
localstorage 的使用比較簡(jiǎn)單,方法有:
localStorage.setItem(key,value);//保存數(shù)據(jù)
localStorage.getItem(key);//讀取數(shù)據(jù)
localStorage.removeItem(key);//刪除單個(gè)數(shù)據(jù)
localStorage.clear();//刪除所有數(shù)據(jù)
key:localStorage.key(index);//得到某個(gè)索引的值
一個(gè)小demo來展示功能:
(function($){
$(function(){
$.fn.getFormParam=function(){
var serializeObj={};
var array=this.serializeArray();
var str=this.serialize();
$(array).each(function(){
if(serializeObj[this.name]){
if($.isArray(serializeObj[this.name])){
serializeObj[this.name].push(this.value);
}else{
serializeObj[this.name]=[serializeObj[this.name],this.value];
}
}else{
serializeObj[this.name]=this.value;
}
});
return serializeObj;
};</p> <p> var storageFile =JSON.parse(window.localStorage.getItem('demo'));
$.each(storageFile, function(i, val){
$('#demoForm').find('[name="'+i+'"]').val(val);
});</p> <p> $('#demoForm').find('[type="submit"]').on('click', function(){
var data = $('#demoForm').getFormParam();
window.localStorage.setItem('demo', JSON.stringify(data));
return false;
});
});
})(jQuery)
html 代碼:
<!doctype html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<script src="jquery-1.10.2.min.js"></script>
<script src="demo.js"></script>
<title>Document</title>
</head>
<body>
<form id="demoForm">
<p><label><span>姓名</span><input name="name"></label></p>
<p><label><span>年齡</span><input name="age"></label></p>
<p><label><span>學(xué)號(hào)</span><input name="number"></label></p>
<p><label><span>地址</span><input name="address"></label></p>
<p><label><span>愛好</span><input name="habit"></label></p>
<p><label><span>其他</span><textarea name="big" id="" cols="30" rows="10"></textarea></label></p>
<p><input type="submit" value="提交"></p>
</form>
</body>
</html>
這樣,一個(gè)簡(jiǎn)單的展示localstorage 的 demo就實(shí)現(xiàn)了
2、sessionStorage
sessionStorage用法與localStorage用法相同,不過sessionStorage在瀏覽器關(guān)閉網(wǎng)站時(shí)候就會(huì)清除,而localStorage會(huì)一直保存至瀏覽器中,二者酌情配合使用。
3、本地?cái)?shù)據(jù)庫
熟悉IOS/Android開發(fā)的同學(xué),應(yīng)該會(huì)對(duì)SQLite數(shù)據(jù)庫比較熟悉
html5中對(duì)數(shù)據(jù)庫的操作比較簡(jiǎn)單,主要有openDatabase方法和transaction方法
用一個(gè)對(duì)象db來接收openDatabase創(chuàng)建的訪問數(shù)據(jù)庫的對(duì)象
var db = openDatabase(databasename,version,description,size)
其中
databasename:數(shù)據(jù)庫名
version:數(shù)據(jù)庫版本 可不填
desription:數(shù)據(jù)庫描述
size:數(shù)據(jù)庫分配空間大小
transaction方法用一個(gè)回調(diào)函數(shù)作為參數(shù),在函數(shù)中執(zhí)行具體的訪問數(shù)據(jù)庫的方法
db.transaction(function(tx)){
tx.executeSql(sqlQuery,[value1,value2..],dataHandler,errorHandler)
});
executeSql方法的四個(gè)參數(shù)分別是:
sqlQuery:需要具體執(zhí)行的sql語句,create||select||update||delete;
[value1,value2..]:sql語句中所有使用到的參數(shù)的數(shù)組,在executeSql方法中,將sql語句中所要使用的參數(shù)先用“?”代替,然后依次將這些參數(shù)組成數(shù)組放在第二個(gè)參數(shù)中;
dataHandler:執(zhí)行成功回調(diào)函數(shù);
errorHandler:執(zhí)行失敗回調(diào)函數(shù);
相關(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è)谧鲰撁鏁r(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-09
HTML5 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