問題
瀏覽器如何存儲信息慢哈?
使用cookie 和 localstorage
那個是永久存儲购裙?
都可以永久儲存宇立,但是cookies可以set expire date
最大的不同
localStorage stays on the client, while cookies are sent with the HTTP header.
cookie 會被瀏覽器自動刪除慨飘,通常存在以下幾種原因:
- 會話 cookie (Session cookiie) 在會話結束時(瀏覽器關閉)會被刪除
- 持久化 cookie(Persistent cookie)在到達失效日期時會被刪除
- 如果瀏覽器中的 cookie 數(shù)量達到限制,那么 cookie 會被刪除以為新建的 cookie 創(chuàng)建空間国撵。
Note:
- 沒有設置 expires 選項時,cookie 的生命周期僅限于當前會話中玻墅,關閉瀏覽器意味著這次會話的結束介牙,所以會話 cookie 僅存在于瀏覽器打開狀態(tài)之下。
- Cookie 會自動出現(xiàn)在client 和 server之間的請求中澳厢,以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