上一篇:HTML5筆記2——HTML5音/視頻標簽詳解
Web Storage概述
在HTML5中,除了Canvas元素之外,另一個新增的非常重要的功能是可以再客戶端本地保存數(shù)據(jù)的Web Storage功能,之前可以使用Cookies在客戶端
保存諸如用戶名等簡單用戶信息,但通過長期使用,人們發(fā)現(xiàn)使用Cookies存儲永久數(shù)據(jù)存在幾個問題。
-
大小:Cookies的大小被限制在4KB
-
帶寬:Cookies是隨HTTP失誤一起被發(fā)送的,因此會浪費一部分發(fā)送Cookies時使用的帶寬
-
復雜性:要正確的操縱Cookies是很困難的。
針對以上問題,HTML5中,重新提供了一中在客戶端本地保存數(shù)據(jù)的功能,他就是Web Storage。
Web Storage功能。
顧名思義,Web Storage功能就是在Web上存儲數(shù)據(jù)的功能,這里的存儲是針對客戶端本地而言的。具體分為兩種:
sessionStorage:將數(shù)據(jù)保存在session對象中。session是指用戶在瀏覽某個網(wǎng)站時,從進入網(wǎng)站到瀏覽器關閉所經(jīng)過的這段時間,也就是用戶瀏
覽這個網(wǎng)站所花費的時間。session對象可以用來保存在這段時間內(nèi)所要保存的任何數(shù)據(jù)。
localStorage:將數(shù)據(jù)保存在客戶端本地的硬件設備(硬盤)中,即使瀏覽器被關閉了,該數(shù)據(jù)仍然存在,下一次打開瀏覽器訪問網(wǎng)站時仍然可以
繼續(xù)使用。localstorage 是通過鍵值對來存儲的。
開發(fā)工具我使用HBuilder.exe
新建Test.html頁面,代碼如下:


<html><head><title></title><meta charset="UTF-8" /><script type="text/javascript">function saveSessiontorage(id) {var targat = document.getElementById(id);var str = targat.value; sessionStorage.setItem("msg", str); }function getSessionStorage(id) {var targat = document.getElementById(id);var msg = sessionStorage.getItem("msg"); targat.innerHTML = msg; }function saveLocalStorage(id) {var targat = document.getElementById(id);var str = targat.value; localStorage.setItem("msg", str); }function getLocalStorage(id) {var targat = document.getElementById(id);var msg = localStorage.getItem("msg"); targat.innerHTML = msg; }</script></head><body><p id="msg"></p><input type="text" id="txt" /><input type="button" value="存儲數(shù)據(jù)" onclick="saveSessiontorage('txt')" /><input type="button" value="讀取數(shù)據(jù)" onclick="getSessionStorage('msg')" /><p id="msg1"></p><p> <input type="text" id="txt1" /></p><input type="button" value="Local存儲數(shù)據(jù)" onclick="saveLocalStorage('txt1')" /><input type="button" value="Local讀取數(shù)據(jù)" onclick="getLocalStorage('msg1')" /></body></html>
View Code
localStorage關閉瀏覽器之后再打開,讀取數(shù)據(jù)依舊存在,而sessionStorage關閉瀏覽器之后再打開讀取數(shù)據(jù)就不見了。
作為簡單數(shù)據(jù)庫來利用
將Web Storage作為簡易數(shù)據(jù)庫,如果能解決數(shù)據(jù)檢索,對列進行管理,就可以將Web Storage作為數(shù)據(jù)庫來利用了。
新建Register.html頁面,代碼如下:


<!DOCTYPE html><html><head><meta charset="UTF-8"><title></title><script type="application/javascript">function addUser () {var data=new Object; data.name=document.getElementById("txtName").value; data.phone=document.getElementById("txtPhone").value; data.address=document.getElementById("txtAddress").value;var str=JSON.stringify(data); localStorage.setItem(data.name,str); alert("注冊成功"); }function search (txt) {var filed=document.getElementById(txt).value;var str=localStorage.getItem(filed);var data=JSON.parse(str);var result="用戶名:"+data.name+"</br>"+"電話:"+data.phone+"</br>"+"地址:"+data.address document.getElementById("txtMsg").innerHTML=result; }</script></head><body><div>用戶名:<input type="text" id="txtName" /></div><div>電話號碼:<input type="text" id="txtPhone" /></div><div>地址:<input type="text" id="txtAddress" /></div><div><input type="button" value="注冊" onclick="addUser()"></div><br /><div>用戶名:<input type="text" id="txtSearch"><input type="button" value="Search" onclick="search('txtSearch')"/></div><div id="txtMsg"></div></body></html>
View Code
HTML5 本地數(shù)據(jù)庫
在HTML5中,大大豐富了客戶端本地可以存儲的內(nèi)容,添加了很多功能將原本必須要保存在服務器上的數(shù)據(jù)轉(zhuǎn)為保存在客戶端本地,從而大大提高了Web應用程序性能,減輕了服務器的負擔,使用Web時代重新回到了“客戶端為重、服務器端為輕”的時代。在HTML5中內(nèi)置了兩種本地數(shù)據(jù)庫,一種為SQLLite,一種為indexedDB。
用executeSql來執(zhí)行查詢
1.transaction.executeSql(sqlquery,[],dataHandler,errorHandler);
2.function dataHandler(transaction,results);
3.function errorHandler(transaction,errmsg);
4.rows.length獲取記錄的條數(shù)
新建SqlTest.html,代碼如下:
<!DOCTYPE html><html><head><meta charset="UTF-8"><title></title></head><body><script type="application/javascript">var db=openDatabase("mydb","1.0","test db",1024*100); //參數(shù)分別為:(數(shù)據(jù)庫名稱,版本號,描述,大小) 如果數(shù)據(jù)庫不存在則創(chuàng)建// db.transaction(function(tx) {// tx.executeSql("")// }) transaction.executeSql("CREATE TABLE IF NOT EXISTS MsgData(name TEXT,msg,TEXT,createtime INTERGER)",[],function(){},function(){});//參數(shù):(sql語句,sql參數(shù)數(shù)組,執(zhí)行成功的回調(diào)函數(shù),執(zhí)行失敗的回調(diào)函數(shù))</script></body></html>
HTML5 indexedDB數(shù)據(jù)庫
在HTML5中,新增了一種被稱為“indexedDB”的數(shù)據(jù)庫,該數(shù)據(jù)庫是一種存儲在客戶端本地的NoSQL數(shù)據(jù)庫。
新建IndexedDBTest.html,代碼如下:


<!DOCTYPE html><html><head><meta charset="UTF-8"><title></title><script type="application/javascript">//統(tǒng)一IndexedDB在不同瀏覽器的實現(xiàn) window.indexedDB = window.indexedDB ||window.mozIndexedDB ||window.webkitIndexedDB ||window.msIndexedDB; window.IDBTransaction = window.IDBTransaction ||window.webkitIDBTransaction ||window.msIDBTransaction; window.IDBKeyRange = window.IDBKeyRange ||window.webkitIDBKeyRange ||window.msIDBKeyRange; function connDB () {var dbName="indexedDBTest";var dbVersion=1;var idb;var dbConnect=indexedDB.open(dbName,dbVersion); dbConnect.onsuccess=function (e) { idb=e.target.result; alert("數(shù)據(jù)庫連接成功!") } dbConnect.onerror=function(e){ alert("數(shù)據(jù)庫連接失?。?quot;); } }</script></head><body><input type="button" value="連接數(shù)據(jù)庫" onclick="connDB()"/></body></html>
View Code
數(shù)據(jù)庫的版本更新
只是成功鏈接數(shù)據(jù)庫,我們還不能執(zhí)行任何數(shù)據(jù)操作,我們還應該創(chuàng)建相當于關系型數(shù)據(jù)庫中數(shù)據(jù)表的對象倉庫與用于檢索數(shù)據(jù)的索引。
新建versionUpdate.html,代碼如下:


<!DOCTYPE html><html><head><meta charset="UTF-8"><title></title><script type="application/javascript">//統(tǒng)一IndexedDB在不同瀏覽器的實現(xiàn) window.indexedDB = window.indexedDB ||window.mozIndexedDB ||window.webkitIndexedDB ||window.msIndexedDB; window.IDBTransaction = window.IDBTransaction ||window.webkitIDBTransaction ||window.msIDBTransaction; window.IDBKeyRange = window.IDBKeyRange ||window.webkitIDBKeyRange ||window.msIDBKeyRange; function VersionUpdate () {var dbName="indexedDBTest";var dbVersion=2;var idb;var dbConnect=indexedDB.open(dbName,dbVersion); dbConnect.onsuccess=function (e) { idb=e.target.result; alert("數(shù)據(jù)庫連接成功!") } dbConnect.onerror=function(e){ alert("數(shù)據(jù)庫連接失??!"); } dbConnect.onupgradeneeded=function(e){ idb=e.target.result;var ts=e.target.transaction;var oldVersion=e.oldVersion;var newVersion=e.newVersion; alert("數(shù)據(jù)庫更新成功!舊版本號:"+oldVersion+"------新版本號:"+newVersion); } }</script></head><body><input type="button" value="更新數(shù)據(jù)庫" onclick="VersionUpdate()" /></body></html>
View Code
創(chuàng)建對象倉庫
對于創(chuàng)建對象倉庫與索引的操作,我們只能在版本更新事務內(nèi)部進行,因為在indexedDB API中不允許數(shù)據(jù)庫中的對象倉庫在同一個版本中發(fā)生改變。
新建createStorge.html,代碼如下:


<!DOCTYPE html><html><head><meta charset="UTF-8"><title></title><script type="application/javascript">//統(tǒng)一IndexedDB在不同瀏覽器的實現(xiàn) window.indexedDB = window.indexedDB ||window.mozIndexedDB ||window.webkitIndexedDB ||window.msIndexedDB; window.IDBTransaction = window.IDBTransaction ||window.webkitIDBTransaction ||window.msIDBTransaction; window.IDBKeyRange = window.IDBKeyRange ||window.webkitIDBKeyRange ||window.msIDBKeyRange; function CreateStorge () {var dbName="indexedDBTest";var dbVersion=2;var idb;var dbConnect=indexedDB.open(dbName,dbVersion); dbConnect.onsuccess=function (e) { idb=e.target.result; alert("數(shù)據(jù)庫連接成功!") } dbConnect.onerror=function(e){ alert("數(shù)據(jù)庫連接失??!"); } dbConnect.onupgradeneeded=function(e){ idb=e.target.result;var name="user";var optionParams={keyPath:"userid",autoIncrement:false};var store=idb.createObjectStore(name,optionParams); alert("對象倉庫創(chuàng)建成功!"); } }</script></head><body><input type="button" value="創(chuàng)建對象倉庫" onclick="CreateStorge()" /></body></html>
View Code