Questions tagged [local-storage]

LocalStorage is a way to store persistent data using JavaScript (see also: SessionStorage).

LocalStorage is a way to store persistent data using JavaScript. It should be used only with HTML5 compatible web browser. Only plain-text values can be stored. Arrays, hashes, numbers, strings and booleans can be stored by using JSON.stringify(value). Then, to get the original value when reading the value, use JSON.parse(stringified_value).

Key-value pairs in localStorage can only be read by client-side code from the same domain.

Example:

localStorage.setItem('key', 'value');
alert(localStorage.getItem('key'));   // Shows "value"
localStorage.removeItem('key');

It is recommended to not use localStorage['key_name'] = 'value'; to assign a variable. Otherwise, property conflicts can occur. For example, imagine what can happen if you want to store a preference called 'setItem'.

Questions

Resources

8547 questions
2725
votes
23 answers

Storing Objects in HTML5 localStorage

I'd like to store a JavaScript object in HTML5 localStorage, but my object is apparently being converted to a string. I can store and retrieve primitive JavaScript types and arrays using localStorage, but objects don't seem to work. Should they?…
Kristopher Johnson
  • 76,675
  • 54
  • 235
  • 299
1170
votes
8 answers

Local Storage vs Cookies

I want to reduce load times on my websites by moving all cookies into local storage since they seem to have the same functionality. Are there any pros/cons (especially performance-wise) in using local storage to replace cookie functionality except…
Gio Borje
  • 16,826
  • 7
  • 33
  • 50
811
votes
12 answers

Clearing localStorage in javascript?

Is there any way to reset/clear browser's localStorage in javascript?
Bnicholas
  • 12,007
  • 6
  • 21
  • 26
711
votes
6 answers

How do I store an array in localStorage?

If I didn't need localStorage, my code would look like this: var names=new Array(); names[0]=prompt("New member name?"); This works. However, I need to store this variable in localStorage and it's proving quite stubborn. I've tried: var…
David
  • 11,601
  • 15
  • 39
  • 51
620
votes
11 answers

HTML5 Local storage vs. Session storage

Apart from being non persistent and scoped only to the current window, are there any benefits (performance, data access, etc) to Session Storage over Local Storage?
jpkeisala
  • 7,585
  • 7
  • 28
  • 38
617
votes
8 answers

What is the difference between localStorage, sessionStorage, session and cookies?

What are the technical pros and cons of localStorage, sessionStorage, session and cookies, and when would I use one over the other?
Pank
  • 11,772
  • 10
  • 30
  • 45
601
votes
12 answers

What is the max size of localStorage values?

Since localStorage (currently) only supports strings as values, and in order to do that the objects need to be stringified (stored as JSON-string) before they can be stored, is there a defined limitation regarding the length of the values. Does…
Codekie
  • 6,585
  • 3
  • 16
  • 24
438
votes
20 answers

How to delete a localStorage item when the browser window/tab is closed?

My Case: localStorage with key + value that should be deleted when browser is closed and not single tab. Please see my code if its proper and what can be improved: //create localStorage key + value if not exist if(localStorage){ …
Ben
  • 23,101
  • 33
  • 104
  • 161
372
votes
17 answers

When do items in HTML5 local storage expire?

For how long is data stored in localStorage (as part of DOM Storage in HTML5) available? Can I set an expiration time for the data which I put into local storage?
user280427
  • 3,731
  • 2
  • 14
  • 4
325
votes
15 answers

How to check whether a Storage item is set?

How can I check if an item is set in localStorage? Currently I am using if (!(localStorage.getItem("infiniteScrollEnabled") == true || localStorage.getItem("infiniteScrollEnabled") == false)) { // init variable/set default variable for item …
Jiew Meng
  • 74,635
  • 166
  • 442
  • 756
272
votes
5 answers

How to view or edit localStorage

I created a Chrome extension and am using localStorage for storing data. I am accessing localStorage through "background_page". It works fine but how can I manually view its values? In Firefox you can use Firebug. Anyone have any suggestions?
Joe Doe
  • 3,234
  • 3
  • 16
  • 17
255
votes
7 answers

Android webview & localStorage

I have a problem with a webview which may access to the localStorage by an HTML5 app. The test.html file informs me that local storage is'nt supported by my browser (ie. the webview). If you have any suggestion.. package com.test.HelloWebView;…
Thomas
  • 8,159
  • 3
  • 14
  • 7
223
votes
9 answers

QuotaExceededError: Dom exception 22: An attempt was made to add something to storage that exceeded the quota

Using LocalStorage on iPhone with iOS 7 throws this error. I've been looking around for a resolvant, but considering I'm not even browsing in private, nothing is relevant. I don't understand why localStorage would be disabled by default in iOS 7,…
Nict
  • 2,946
  • 5
  • 21
  • 35
201
votes
8 answers

Is it safe to store a JWT in localStorage with ReactJS?

I'm currently building a single page application using ReactJS. I read that one of the reasons for not using localStorage is because of XSS vulnerabilities. Since React escapes all user input, would it now be safe to use localStorage?
user6127082
189
votes
7 answers

How to view/delete local storage in Firefox?

In Google Chrome there is an easy way to see what's in local storage as well as modify or delete it after inspecting it. Is there a way to do the same in Firefox?
Ryan
  • 8,933
  • 19
  • 57
  • 97
1
2 3
99 100