问题
I am working in an app where I need to keep some data during the user is logged in and I have that question, what is the difference among localStorage, sessionStorage, cookies ???
I was asking what can I use in order to persist some data in the DOM, even if the user refresh the page, some people says: use sessionStorage, or localStorage, then, someone came up with the idea of use ngCookies because it works in every browser, but, which should I use ?
回答1:
localStorage and sessionStorage are both so-called WebStorages and features of HTML5.
localStorage stores information as long as the user does not delete them.
sessionStorage stores information as long as the session goes. Usually until the user closes the tab/browser.
cookies are simply cookies, which are supported by older browsers and usually are a fallback for frameworks that use the above mentioned WebStorages.
In contrast cookies can store way less information then WebStorages and the information in WebStorages is never transferred to the server.
Keep in mind that the EU has a regulation that requires websites to inform their users about the usage of cookies. I dont know whether this also applies to WebStorages
回答2:
sessionStorage object: The sessionStorage object stores data only for a session, meaning that the data is stored until the browser (or tab) is closed. it is not available when a file is run locally.
Data stored in the sessionStorage object is accessible only from the page that initially stored the data; so this doesn't meet your requirement
localStorage object: Data stored using the localStorage object is persisted until it is specifically removed via JavaScript or the user clears the browser’s cache.
Data stored in the localStorage object is accessible only from the domain that initially stored the data.
For your case, I think you make consider about using cookie or session, pls. note cookie has 4K size limitation per server.
回答3:
Addition to other answers, WebStorages cannot access subdomain and/or parent domain.
回答4:
localStorage : 1. Data Limit : 5 MB 2. Data sent for every http request : no
sessionStorage : 1. Data Limit : 5 MB 2. Data sent for every http request : no 3. Data will be cleared once window or tab is closed
I would say, use localstorage/sessionStorage if data is non sensitive else use cookies
回答5:
LocalStorage - Stores data with no expiration date, and gets cleared only through JavaScript, or clearing the Browser cache / Locally Stored Data. Storage limit is the maximum amongst the three
SessionStorage - The sessionStorage object stores data only for a session, meaning that the data is stored until the browser (or tab) is closed. Data is never transferred to the server. Storage limit is larger than a cookie (at least 5MB).
Cookie - Stores data that has to be sent back to the server with subsequent requests. Its expiration varies based on the type and the expiration duration can be set from either server-side or client-side (normally from server-side). Cookies are primarily for server-side reading (can also be read on client-side), localStorage and sessionStorage can only be read on client-side. Size must be less than 4KB. Cookies can be made secure by setting the httpOnly flag as true for that cookie. This prevents client-side access to that cookie.
回答6:
Cookies are just hold 4kbs data and as expiry time.
localStorage are permanent cookies holds 4 MB data it will delete when user clears cache
来源:https://stackoverflow.com/questions/29960037/localstorage-vs-sessionstorage-vs-cookies