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 ?
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
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.
Addition to other answers, WebStorages cannot access subdomain and/or parent domain.
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
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