1.html5 web存儲(chǔ)
早些時(shí)候,本地存儲(chǔ)使用的是 cookie。但是Web 存儲(chǔ)需要更加的安全與快速. 這些數(shù)據(jù)不會(huì)被保存在服務(wù)器上,但是這些數(shù)據(jù)只用于用戶(hù)請(qǐng)求網(wǎng)站數(shù)據(jù)上.它也可以存儲(chǔ)大量的數(shù)據(jù),而不影響網(wǎng)站的性能.
數(shù)據(jù)以 鍵/值 對(duì)存在, web網(wǎng)頁(yè)的數(shù)據(jù)只允許該網(wǎng)頁(yè)訪問(wèn)使用。
2.兩個(gè)對(duì)象
- localStorage - 沒(méi)有時(shí)間限制的數(shù)據(jù)存儲(chǔ)
- sessionStorage - 針對(duì)一個(gè) session 的數(shù)據(jù)存儲(chǔ)(關(guān)閉窗口,存儲(chǔ)的數(shù)據(jù)清空)
setItem存儲(chǔ)value
用途:將value存儲(chǔ)到key字段
用法:.setItem( key, value)
sessionStorage.setItem("key", "value"); localStorage.setItem("site", "js8.in");
getItem獲取value
用途:獲取指定key本地存儲(chǔ)的值
用法:.getItem(key)
var value = sessionStorage.getItem("key");
var site = localStorage.getItem("site");