browser storage

問(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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容