LocalStorage vs sessionStorage vs cookie

I work in an application where I need to save some data during login, and I have this question, what is the difference between localStorage, sessionStorage, cookies ???

I asked what I can use to save some data in the DOM, even if the user refreshes the page, some say: use sessionStorage or localStorage, then someone came up with the idea of ​​using ngCookies because it works in every browser, but what should I use ?

+47
angularjs html5 local-storage cookies session-cookies
Apr 30 '15 at 5:54
source share
5 answers

localStorage and sessionStorage are the so-called WebStorages and HTML5 functions.

localStorage stores information until the user deletes them.

sessionStorage stores information as long as the session is in progress. Usually, until the user closes the tab / browser.

cookies are simply cookies that are supported by older browsers and are usually fallback for frameworks that use the aforementioned WebStorages .

In contrast, cookies can store less information than WebStorages, and information in WebStorages is never transferred to the server.

Keep in mind that there is a rule in the EU that requires websites to inform their users about the use of cookies. I don't know if this also applies to WebStorages

+66
Apr 30 '15 at 5:58
source share

sessionStorage object: The sessionStorage object stores data for the session only, which means that the data is saved until the browser (or tab) closes. it is not available if the file is running locally.

The data stored in the sessionStorage object is available only on the page that originally stored the data; therefore it does not meet your requirement

localStorage: Data stored using the localStorage object is stored until it is deleted using JavaScript or the user clears the browsers cache.

The data stored in the localStorage object is available only from the domain that originally stored the data.

In your case, I think you will think about how to use cookies or session, pls. note cookie has a 4K size limit per server.

+16
Apr 30 '15 at 6:08
source share

Addition to other answers, WebStorages cannot access the subdomain and / or parent domain.

+3
Aug 09 '16 at 9:15
source share

localStorage: 1. Data limit: 5 MB 2. Data sent for each request http: no

sessionStorage: 1. Data limit: 5 MB 2. Data sent for each HTTP request: none 3. Data will be deleted after closing the window or tab.

I would say use localstorage / sessionStorage, if data is insensitive use cookie

0
Dec 15 '17 at 13:43 on
source share

Cookies simply store data of 4 kilobytes and as an expiration time.

localStorage are persistent cookies containing 4 MB of data that it will delete when the user clears the cache

-2
Nov 18 '17 at 11:25
source share



All Articles