亚洲最大看欧美片,亚洲图揄拍自拍另类图片,欧美精品v国产精品v呦,日本在线精品视频免费

  • 站長(zhǎng)資訊網(wǎng)
    最全最豐富的資訊網(wǎng)站

    Web Storage概述和本地?cái)?shù)據(jù)庫(kù)

    上一篇:HTML5筆記2——HTML5音/視頻標(biāo)簽詳解

    Web Storage概述

    在HTML5中,除了Canvas元素之外,另一個(gè)新增的非常重要的功能是可以再客戶端本地保存數(shù)據(jù)的Web Storage功能,之前可以使用Cookies在客戶端

    保存諸如用戶名等簡(jiǎn)單用戶信息,但通過(guò)長(zhǎng)期使用,人們發(fā)現(xiàn)使用Cookies存儲(chǔ)永久數(shù)據(jù)存在幾個(gè)問(wèn)題。

    • 大小:Cookies的大小被限制在4KB

    • 帶寬:Cookies是隨HTTP失誤一起被發(fā)送的,因此會(huì)浪費(fèi)一部分發(fā)送Cookies時(shí)使用的帶寬

    • 復(fù)雜性:要正確的操縱Cookies是很困難的。

    針對(duì)以上問(wèn)題,HTML5中,重新提供了一中在客戶端本地保存數(shù)據(jù)的功能,他就是Web Storage。

    Web Storage功能。

    顧名思義,Web Storage功能就是在Web上存儲(chǔ)數(shù)據(jù)的功能,這里的存儲(chǔ)是針對(duì)客戶端本地而言的。具體分為兩種:

    sessionStorage:將數(shù)據(jù)保存在session對(duì)象中。session是指用戶在瀏覽某個(gè)網(wǎng)站時(shí),從進(jìn)入網(wǎng)站到瀏覽器關(guān)閉所經(jīng)過(guò)的這段時(shí)間,也就是用戶瀏

    覽這個(gè)網(wǎng)站所花費(fèi)的時(shí)間。session對(duì)象可以用來(lái)保存在這段時(shí)間內(nèi)所要保存的任何數(shù)據(jù)。

    localStorage:將數(shù)據(jù)保存在客戶端本地的硬件設(shè)備(硬盤)中,即使瀏覽器被關(guān)閉了,該數(shù)據(jù)仍然存在,下一次打開瀏覽器訪問(wèn)網(wǎng)站時(shí)仍然可以

    繼續(xù)使用。localstorage 是通過(guò)鍵值對(duì)來(lái)存儲(chǔ)的。

    開發(fā)工具我使用HBuilder.exe

    新建Test.html頁(yè)面,代碼如下:

    Web Storage概述和本地?cái)?shù)據(jù)庫(kù)Web Storage概述和本地?cái)?shù)據(jù)庫(kù)

    <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="存儲(chǔ)數(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存儲(chǔ)數(shù)據(jù)" onclick="saveLocalStorage('txt1')" /><input type="button" value="Local讀取數(shù)據(jù)" onclick="getLocalStorage('msg1')" /></body></html>

    View Code

    Web Storage概述和本地?cái)?shù)據(jù)庫(kù)

    localStorage關(guān)閉瀏覽器之后再打開,讀取數(shù)據(jù)依舊存在,而sessionStorage關(guān)閉瀏覽器之后再打開讀取數(shù)據(jù)就不見了。

    作為簡(jiǎn)單數(shù)據(jù)庫(kù)來(lái)利用

    將Web Storage作為簡(jiǎn)易數(shù)據(jù)庫(kù),如果能解決數(shù)據(jù)檢索,對(duì)列進(jìn)行管理,就可以將Web Storage作為數(shù)據(jù)庫(kù)來(lái)利用了。

    新建Register.html頁(yè)面,代碼如下:

    Web Storage概述和本地?cái)?shù)據(jù)庫(kù)Web Storage概述和本地?cái)?shù)據(jù)庫(kù)

    <!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("注冊(cè)成功");              }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>電話號(hào)碼:<input type="text" id="txtPhone" /></div><div>地址:<input type="text" id="txtAddress" /></div><div><input type="button" value="注冊(cè)" 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

    Web Storage概述和本地?cái)?shù)據(jù)庫(kù)

    HTML5 本地?cái)?shù)據(jù)庫(kù)

    在HTML5中,大大豐富了客戶端本地可以存儲(chǔ)的內(nèi)容,添加了很多功能將原本必須要保存在服務(wù)器上的數(shù)據(jù)轉(zhuǎn)為保存在客戶端本地,從而大大提高了Web應(yīng)用程序性能,減輕了服務(wù)器的負(fù)擔(dān),使用Web時(shí)代重新回到了“客戶端為重、服務(wù)器端為輕”的時(shí)代。在HTML5中內(nèi)置了兩種本地?cái)?shù)據(jù)庫(kù),一種為SQLLite,一種為indexedDB。

    用executeSql來(lái)執(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ù)庫(kù)名稱,版本號(hào),描述,大小) 如果數(shù)據(jù)庫(kù)不存在則創(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語(yǔ)句,sql參數(shù)數(shù)組,執(zhí)行成功的回調(diào)函數(shù),執(zhí)行失敗的回調(diào)函數(shù))</script></body></html>

    HTML5 indexedDB數(shù)據(jù)庫(kù)

    在HTML5中,新增了一種被稱為“indexedDB”的數(shù)據(jù)庫(kù),該數(shù)據(jù)庫(kù)是一種存儲(chǔ)在客戶端本地的NoSQL數(shù)據(jù)庫(kù)。

    新建IndexedDBTest.html,代碼如下:

    Web Storage概述和本地?cái)?shù)據(jù)庫(kù)Web Storage概述和本地?cái)?shù)據(jù)庫(kù)

    <!DOCTYPE html><html><head><meta charset="UTF-8"><title></title><script type="application/javascript">//統(tǒng)一IndexedDB在不同瀏覽器的實(shí)現(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ù)庫(kù)連接成功!")                      }                      dbConnect.onerror=function(e){                          alert("數(shù)據(jù)庫(kù)連接失??!");                      }                  }</script></head><body><input type="button" value="連接數(shù)據(jù)庫(kù)"  onclick="connDB()"/></body></html>

    View Code

    Web Storage概述和本地?cái)?shù)據(jù)庫(kù)

    數(shù)據(jù)庫(kù)的版本更新

    只是成功鏈接數(shù)據(jù)庫(kù),我們還不能執(zhí)行任何數(shù)據(jù)操作,我們還應(yīng)該創(chuàng)建相當(dāng)于關(guān)系型數(shù)據(jù)庫(kù)中數(shù)據(jù)表的對(duì)象倉(cāng)庫(kù)與用于檢索數(shù)據(jù)的索引。

    新建versionUpdate.html,代碼如下:

    Web Storage概述和本地?cái)?shù)據(jù)庫(kù)Web Storage概述和本地?cái)?shù)據(jù)庫(kù)

    <!DOCTYPE html><html><head><meta charset="UTF-8"><title></title><script type="application/javascript">//統(tǒng)一IndexedDB在不同瀏覽器的實(shí)現(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ù)庫(kù)連接成功!")                      }                      dbConnect.onerror=function(e){                          alert("數(shù)據(jù)庫(kù)連接失??!");                      }                      dbConnect.onupgradeneeded=function(e){                          idb=e.target.result;var ts=e.target.transaction;var oldVersion=e.oldVersion;var newVersion=e.newVersion;                          alert("數(shù)據(jù)庫(kù)更新成功!舊版本號(hào):"+oldVersion+"------新版本號(hào):"+newVersion);                      }                  }</script></head><body><input type="button" value="更新數(shù)據(jù)庫(kù)" onclick="VersionUpdate()" /></body></html>

    View Code

    Web Storage概述和本地?cái)?shù)據(jù)庫(kù)Web Storage概述和本地?cái)?shù)據(jù)庫(kù)

    創(chuàng)建對(duì)象倉(cāng)庫(kù)

    對(duì)于創(chuàng)建對(duì)象倉(cāng)庫(kù)與索引的操作,我們只能在版本更新事務(wù)內(nèi)部進(jìn)行,因?yàn)樵趇ndexedDB API中不允許數(shù)據(jù)庫(kù)中的對(duì)象倉(cāng)庫(kù)在同一個(gè)版本中發(fā)生改變。

    新建createStorge.html,代碼如下:

    Web Storage概述和本地?cái)?shù)據(jù)庫(kù)Web Storage概述和本地?cái)?shù)據(jù)庫(kù)

    <!DOCTYPE html><html><head><meta charset="UTF-8"><title></title><script type="application/javascript">//統(tǒng)一IndexedDB在不同瀏覽器的實(shí)現(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ù)庫(kù)連接成功!")                      }                      dbConnect.onerror=function(e){                          alert("數(shù)據(jù)庫(kù)連接失??!");                      }                      dbConnect.onupgradeneeded=function(e){                          idb=e.target.result;var name="user";var optionParams={keyPath:"userid",autoIncrement:false};var store=idb.createObjectStore(name,optionParams);                          alert("對(duì)象倉(cāng)庫(kù)創(chuàng)建成功!");                      }                  }</script></head><body><input type="button" value="創(chuàng)建對(duì)象倉(cāng)庫(kù)" onclick="CreateStorge()" /></body></html>

    View Code

    贊(0)
    分享到: 更多 (0)
    網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)