方法屬性 |
描述 |
setItem(key,value) |
該方法接收一個鍵名和值作為參數(shù),將會把鍵值對添加到存儲中,如果鍵名存在,則更新其對應的值 |
getItem(key) |
該方法接收一個鍵名作為參數(shù),返回鍵名對應的值 |
romoveItem(key) |
該方法接收一個鍵名作為參數(shù),并把該鍵名從存儲中刪除 |
length |
類似數(shù)組的length屬性,用于訪問Storage對象中item的數(shù)量 |
key(n) |
用于訪問第n個key的名稱 |
clear() |
清除當前域下的所有l(wèi)ocalSotrage內(nèi)容 |
代碼示例:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>localStorage</title> </head> <body> <input type="text" id="username" > <input type="button" value="localStorage 設(shè)置數(shù)據(jù) " id="localStorageId"> <input type="button" value="localStorage 獲取數(shù)據(jù) " id="getlocalStorageId"> <input type="button" value="localStorage 刪除數(shù)據(jù) " id="removesessionStorageId"> <script> document.getElementById("localStorageId").onclick=function(){ // 把用戶在 input 里面數(shù)據(jù)的數(shù)據(jù)保存到 localStorage var username=document.getElementById("username").value; window.localStorage.setItem("username",username); }; document.getElementById("getlocalStorageId").onclick=function(){ // 獲取數(shù)據(jù),從 localStorage var username=window.localStorage.getItem("username"); alert(username); }; document.getElementById("removesessionStorageId").onclick=function(){ // 刪除 localStorage 中的數(shù)據(jù) var username=document.getElementById("username").value; window.localStorage.removeItem("username"); }; </script> </body> </html>
sessionStorage 主要用于區(qū)域存儲,區(qū)域存儲是指數(shù)據(jù)只在單個頁面的會話期內(nèi)有效。由于 sessionStroage 也是 Storage 的實例, sessionStroage 與 localStorage 中的方法基本一致,唯一區(qū)別就是存儲數(shù)據(jù)的生命周期不同, locaStorage 是永久性存儲,而 sessionStorage 的生命周期與會話保持一致,會話結(jié)束時數(shù)據(jù)消失。
2.3sessionStorage實現(xiàn)區(qū)域存儲
從硬件方面理解, localStorage 的數(shù)據(jù)是存儲子在硬盤中的,關(guān)閉瀏覽器時數(shù)據(jù)仍然在硬盤上,再次打開瀏覽器仍然可以獲取,而 sessionStorage 的數(shù)據(jù)保存在瀏覽器的內(nèi)存中,當瀏覽器關(guān)閉后,內(nèi)存將被自動清除,需要注意的是, sessionStorage 中存儲的數(shù)據(jù)只在當前瀏覽器窗口有效。
代碼示例:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>sessionStorage</title> </head> <body> 姓名: <input type="text" id="username"> 年齡: <input type="text" id="age"> <input type="button" value="sessionStorage 設(shè)置數(shù)據(jù) " 11 id="sessionStorageId"> <input type="button" value="sessionStorage 獲取數(shù)據(jù) " id="getsessionStorageId"> <input type="button" value="sessionStorage 清除所有數(shù)據(jù) " id="clearsessionStorageId"> <script> // 增加數(shù)據(jù) document.getElementById("sessionStorageId").onclick = function() { // 獲取姓名和年齡輸入框的值 var username = document.getElementById("username").value; var age = document.getElementById("age").value; // 定義一個 user 對象用來保存獲取的信息 var user = { username: username, age: age } // 使用 stringify() 將 JSON 對象序列號并存入到 sessionStorage window.sessionStorage.setItem("user",JSON.stringify(user)); }; //sessionStorage 里面存儲數(shù)據(jù),如果關(guān)閉了瀏覽器,數(shù)據(jù)就會消失 .. // 單個瀏覽器窗口頁面有效 document.getElementById("getsessionStorageId").onclick = function() { var valu = window.sessionStorage.getItem("user"); alert(valu); }; // 清除所有的數(shù)據(jù) document.getElementById("clearsessionStorageId").onclick = function() { window.sessionStorage.clear(); }; </script> </body> </html>
3 總結(jié)
HTML5中的兩種存儲方式都比較實用,我們在設(shè)計前端頁面時,可以根據(jù)相應的用戶訪問情況預測來增添相應的js,既增加了用戶瀏覽的體驗,又能實現(xiàn)存儲管理的高效性,合理的利用存儲空間。
到此這篇關(guān)于HTML5中的網(wǎng)絡(luò)存儲的文章就介紹到這了,更多相關(guān)html5 網(wǎng)絡(luò)存儲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!