Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't localStorage used instead of cookies? ( and in other cases as well )

Tags:

javascript

According to MDN it is suppose to be more secure than cookies for storing persistent data on the client.

However, checking the localStorage of facebook.com, twitter.com, and linkedin.com I can see that it is not being used.

Oddly, linkedin does have the key ( in localStorage ) 8df when logged in , but trying to access it throws an error.

like image 466
Handy Avatar asked Aug 24 '13 21:08

Handy


People also ask

Why would you use a cookie instead of local storage?

Cookies are intended to be read by the server, whereas localStorage can only be read by the browser. Thus, cookies are restricted to small data volumes, while localStorage can store more data.

Can local storage Replace cookies?

The data stored in local storage is only available on the client-side. Unlike cookies, it is not sent to the server on each request. The major difference between cookies and local storage is the amount of data that can be stored.

Why we should not use localStorage?

Limitations & considerations to use local storage: It is not secure, can be accessed by browser developer tools, so don't use it to store sensitive data. It can be cleared by the user when he/she clears all browser history. It can only store string data.

Should you use cookies or localStorage?

If you need to store data that is accessible for both the server and the client, use cookies. Otherwise, use local storage. If you need to store larger data, use local storage. If you need to store data that does not expire, use local storage.


2 Answers

My guess (hopes this qualifies has an answer)

Web Storage is compatible with most common browsers: http://caniuse.com/namevalue-storage .

For things that don't need to transit with session: what probably happens is that cookies is most commonly known and easy to use. There are lots of companies with average skilled ppl, who will run away when confronted with things out of their confort zone.

Edit after Python Fanboy's answer (+1 from me): read his answer.

like image 165
fmsf Avatar answered Sep 21 '22 10:09

fmsf


localStorage has this drawback which cookies doesn't have: it's stored values aren't sent automatically with all HTTP requests so without more implementation Your server won't know what's stored in browser's localStorage.

localStorage is supported in IE since IE8.

like image 39
HankMoody Avatar answered Sep 21 '22 10:09

HankMoody