browser storage

问题

浏览器如何存储信息?

使用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 数量达到限制,那么 cookie 会被删除以为新建的 cookie 创建空间。

Note:

  • 没有设置 expires 选项时,cookie 的生命周期仅限于当前会话中,关闭浏览器意味着这次会话的结束,所以会话 cookie 仅存在于浏览器打开状态之下。
  • Cookie 会自动出现在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

你可能感兴趣的:(browser storage)