問(wèn)題
瀏覽器如何存儲(chǔ)信息?
使用cookie 和 localstorage
那個(gè)是永久存儲(chǔ)?
都可以永久儲(chǔ)存,但是cookies可以set expire date
最大的不同
localStorage stays on the client, while cookies are sent with the HTTP header.
cookie 會(huì)被瀏覽器自動(dòng)刪除,通常存在以下幾種原因:
- 會(huì)話 cookie (Session cookiie) 在會(huì)話結(jié)束時(shí)(瀏覽器關(guān)閉)會(huì)被刪除
- 持久化 cookie(Persistent cookie)在到達(dá)失效日期時(shí)會(huì)被刪除
- 如果瀏覽器中的 cookie 數(shù)量達(dá)到限制,那么 cookie 會(huì)被刪除以為新建的 cookie 創(chuàng)建空間。
Note:
- 沒(méi)有設(shè)置 expires 選項(xiàng)時(shí),cookie 的生命周期僅限于當(dāng)前會(huì)話中,關(guān)閉瀏覽器意味著這次會(huì)話的結(jié)束,所以會(huì)話 cookie 僅存在于瀏覽器打開(kāi)狀態(tài)之下。
- Cookie 會(huì)自動(dòng)出現(xiàn)在client 和 server之間的請(qǐng)求中,以header的形式。
Cookies and local storage serve different purposes.
Cookies are primarily for reading server-side
local storage can only be read by the client-side.
So the question is, in your app, who needs this data — the client or the server?
Size
- Cookies give you a limit of 4096 bytes (4095, actually) - its per cookie.
- Local Storage is as big as 5MB per domain.
localStorage is an implementation of the Storage Interface. It stores data with no expiration date, and gets cleared only through JavaScript, or clearing the Browser Cache / Locally Stored Data - unlike cookie expiry.
Reference:
https://stackoverflow.com/questions/3220660/local-storage-vs-cookies
http://bubkoo.com/2014/04/21/http-cookies-explained/
http://cuyu.github.io/web/2017/08/01/%E4%BD%BF%E7%94%A8cookie%E5%AE%9E%E7%8E%B0%E9%A1%B5%E9%9D%A2%E8%87%AA%E5%8A%A8%E7%99%BB%E5%BD%95