在html5之前,瀏覽器要實現資料的存儲,通常都是用cookie,但是cookie有網域名稱和大小限定.
html5流行之後,可以透過localStorage和sessionStorage實現瀏覽器端的資料存儲,這兩者有什麼特色呢?
sessionStorage
sessionStorage屬於臨時會話,資料儲存的有效期為:從頁面開啟到頁面關閉的時間段,屬於視窗的暫存,頁面關閉,本地儲存消失
localStorage
永久儲存(可以手動刪除資料)
儲存量限制( 5M )
客戶端完成,不會請求伺服器處理
sessionStorage資料在頁面之間不能共享、 而localStorage可以實現頁面之間共享
sessionStorage的應用程式:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script> window.onload = function(){ var aInput = document.getElementsByTagName('input'); aInput[0].onclick = function(){ //sessionStorage: 临时存储, 只在当前页面有效,不能传递到其他页面,页面关闭之后消失 window.sessionStorage.setItem("name", aInput[3].value ); }; aInput[1].onclick = function(){ alert(window.sessionStorage.getItem("name" )); }; aInput[2].onclick = function(){ window.sessionStorage.removeItem("name" ); }; } </script> </head> <body> <input type="button" value="设置" /> <input type="button" value="获取" /> <input type="button" value="删除" /> <br/> <input type="text" /> </body> </html>
localStorage的應用程式
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script> window.onload = function(){ var aInput = document.getElementsByTagName('input'); aInput[0].onclick = function(){ //localStorage : 永久性存储 window.localStorage.setItem("name", aInput[3].value); window.localStorage.setItem("name2", 'aaaaa'); }; aInput[1].onclick = function(){ alert( window.localStorage.getItem( "name" ) ); alert( window.localStorage.getItem( "name2" ) ); }; aInput[2].onclick = function(){ window.localStorage.removeItem("name"); // window.localStorage.clear(); }; } </script> </head> <body> <input type="button" value="设置" /> <input type="button" value="获取" /> <input type="button" value="删除" /> <br/> <input type="text" /> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script> window.onload = function () { var aInput = document.getElementsByTagName("input"); var oT = document.querySelector("textarea"); if (window.localStorage.getItem("userName")) { aInput[0].value = window.localStorage.getItem("userName"); } for (var i = 0; i < aInput.length; i++) { if (window.localStorage.getItem('sex') == aInput[i].value) { aInput[i].checked = true; } } if (window.localStorage.getItem("note")) { oT.value = window.localStorage.getItem("note"); } window.onunload = function () { if (aInput[0].value) { window.localStorage.setItem("userName", aInput[0].value); } for (var i = 0; i < aInput.length; i++) { if (aInput[i].checked == true) { window.localStorage.setItem('sex', aInput[i].value); } } if (oT.value) { window.localStorage.setItem('note', oT.value); } } } </script> </head> <body> <p> 用户名: <input type="text"/> </p> <p> 性别: <br/> <input type="radio" name="sex" value="男"/>男 <input type="radio" name="sex" value="女"/>女 </p> <p> 备注: <textarea cols="30" rows="10"></textarea> </p> </body> </html>##########
以上是HTML5本機儲存應用sessionStorage和localStorage的詳細內容。更多資訊請關注PHP中文網其他相關文章!

html5isamajorrevisionofthehtmlStandardThatRevolutionsWebDevelopmentBybyIntroDucingNewSemanticeLementSemelementsandAndCapabilities.1)itenhancesCodereAdabilityAndSeowitability andSeowithelientsLike,and.2)

H5的高級技巧包括:1.利用進行複雜圖形繪製,2.使用WebWorkers提升性能,3.通過WebStorage增強用戶體驗,4.實現響應式設計,5.利用WebRTC實現實時通信,6.進行性能優化和最佳實踐。這些技巧幫助開發者構建更動態、互動和高效的Web應用。

H5(HTML5)將通過新元素和API提升網頁內容和設計。 1)H5增強了語義化標記和多媒體支持。 2)它引入了Canvas和SVG,豐富了網頁設計。 3)H5的工作原理是通過新標籤和API擴展HTML功能。 4)基本用法包括使用創建圖形,高級用法涉及WebStorageAPI。 5)開發者需注意瀏覽器兼容性和性能優化。

H5帶來了多項新功能和能力,極大提升了網頁的互動性和開發效率。 1.語義化標籤如、增強了SEO。 2.多媒體支持通過和標籤簡化了音視頻播放。 3.Canvas繪圖提供了動態圖形繪製工具。 4.本地存儲通過localStorage和sessionStorage簡化了數據存儲。 5.地理位置API便於開發基於位置的服務。

HTML5帶來了五個關鍵改進:1.語義化標籤提升了代碼清晰度和SEO效果;2.多媒體支持簡化了視頻和音頻嵌入;3.表單增強簡化了驗證;4.離線與本地存儲提高了用戶體驗;5.畫布與圖形功能增強了網頁的可視化效果。

HTML5的核心特性包括語義化標籤、多媒體支持、離線存儲與本地存儲、表單增強。 1.語義化標籤如、等,提升代碼可讀性和SEO效果。 2.和標籤簡化多媒體嵌入。 3.離線存儲和本地存儲如ApplicationCache和LocalStorage,支持無網絡運行和數據存儲。 4.表單增強引入新輸入類型和驗證屬性,簡化處理和驗證。

H5提供了多種新特性和功能,極大地增強了前端開發的能力。 1.多媒體支持:通過和元素嵌入媒體,無需插件。 2.畫布(Canvas):使用元素動態渲染2D圖形和動畫。 3.本地存儲:通過localStorage和sessionStorage實現數據持久化存儲,提升用戶體驗。

H5和HTML5是不同的概念:HTML5是HTML的一個版本,包含新元素和API;H5是基於HTML5的移動應用開發框架。 HTML5通過瀏覽器解析和渲染代碼,H5應用則需要容器運行並通過JavaScript與原生代碼交互。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

Atom編輯器mac版下載
最受歡迎的的開源編輯器

Dreamweaver Mac版
視覺化網頁開發工具

記事本++7.3.1
好用且免費的程式碼編輯器

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境