WebStorage? ?
? ? ? ? ? ? ?Web項(xiàng)目存儲數(shù)據(jù)常用的方案:
? ? ? ? ? ? ?(1)服務(wù)器端存儲
??? ? ? ? ? ??????? 1)數(shù)據(jù)庫存儲,如商品、用戶等核心數(shù)據(jù)
?? ? ? ? ? ???????? 2)Session/內(nèi)存存儲,如用戶的登錄信息
?? ? ? ? ? ? (2)客戶端存儲
??? ? ? ? ? ??????? 3)Cookie存儲,如用戶偏好、訪問歷史,瀏覽器兼容性好但處理麻煩且容量限制
???? ? ? ? ? ?????? 4)H5 WebStorage存儲,如用戶偏好、訪問歷史等安全要求的數(shù)據(jù),老IE不兼容但易使用且容量大
? ? ? ? ? ? ?H5WebStorage存儲具體涉及到兩個(gè)對象:
?? ? ? ? ? ?? ? ? ? ? ? (1)window.sessionStorage:類數(shù)組對象,通過key=>value對存儲字符串?dāng)?shù)據(jù)——會話級存儲
????? ? ? ? ? ?? ? ? ? ? ????? 添加數(shù)據(jù):sessionStorage['key'] = 'value'
???? ? ? ? ? ?? ? ? ? ? ????? 修改數(shù)據(jù):sessionStorage['key'] = 'newValue'
???? ? ? ? ? ?? ? ? ? ? ?????? 刪除數(shù)據(jù):delete sessionStorage['key']
???? ? ? ? ? ?? ? ? ? ? ?????? 獲得數(shù)據(jù):var? v = sessionStorage['key']
?? ? ? ? ? ?? ? ? ? ? ? (2)window.localStorage:類數(shù)組對象,通過key=>value對存儲字符串?dāng)?shù)據(jù)——本地/跨會話級/永久存儲
???? ? ? ? ? ?? ? ? ? ? ?????? 添加數(shù)據(jù):localStorage['key'] = 'value'
????? ? ? ? ? ?? ? ? ? ? ????? 修改數(shù)據(jù):localStorage['key'] = 'newValue'
??????? ? ? ? ? ?? ? ? ? ? ??? 刪除數(shù)據(jù):delete localStorage['key']
????? ? ? ? ? ?? ? ? ? ? ????? 獲得數(shù)據(jù):var? v = localStorage['key']