Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Question

Post Timeline

could also be used as dupe target for sessionStorage
Link
samliew
  • 79.5k
  • 112
  • 174
  • 319

How to store objects in HTML5 localStorage/sessionStorage

Active reading. Expanded. Dressed the naked link.
Source Link
Peter Mortensen
  • 31.3k
  • 22
  • 110
  • 134

How to Store Objectsstore 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?

Here's my code:

var testObject = { 'one': 1, 'two': 2, 'three': 3 };
console.log('typeof testObject: ' + typeof testObject);
console.log('testObject properties:');
for (var prop in testObject) {
 console.log(' ' + prop + ': ' + testObject[prop]);
}
// Put the object into storage
localStorage.setItem('testObject', testObject);
// Retrieve the object from storage
var retrievedObject = localStorage.getItem('testObject');
console.log('typeof retrievedObject: ' + typeof retrievedObject);
console.log('Value of retrievedObject: ' + retrievedObject);

The console output is

typeof testObject: object
testObject properties:
 one: 1
 two: 2
 three: 3
typeof retrievedObject: string
Value of retrievedObject: [object Object]

It looks to me like the setItem method is converting the input to a string before storing it.

I see this behavior in Safari, Chrome, and Firefox, so I assume it's my misunderstanding of the HTML5 Web Storage specspecification, not a browser-specific bug or limitation.

I've tried to make sense of the structured clone algorithm described in http://www.w3.org/TR/html5/infrastructure.html 2 Common infrastructure . I don't fully understand what it's saying, but maybe my problem has to do with my object's properties not being enumerable (???).

Is there an easy workaround?


Update: The W3C eventually changed their minds about the structured-clone specification, and decided to change the spec to match the implementations. See https://www.w3.org/Bugs/Public/show_bug.cgi?id=12111 12111 – spec for Storage object getItem(key) method does not match implementation behavior . So this question is no longer 100% valid, but the answers still may be of interest.

How to Store 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?

Here's my code:

var testObject = { 'one': 1, 'two': 2, 'three': 3 };
console.log('typeof testObject: ' + typeof testObject);
console.log('testObject properties:');
for (var prop in testObject) {
 console.log(' ' + prop + ': ' + testObject[prop]);
}
// Put the object into storage
localStorage.setItem('testObject', testObject);
// Retrieve the object from storage
var retrievedObject = localStorage.getItem('testObject');
console.log('typeof retrievedObject: ' + typeof retrievedObject);
console.log('Value of retrievedObject: ' + retrievedObject);

The console output is

typeof testObject: object
testObject properties:
 one: 1
 two: 2
 three: 3
typeof retrievedObject: string
Value of retrievedObject: [object Object]

It looks to me like the setItem method is converting the input to a string before storing it.

I see this behavior in Safari, Chrome, and Firefox, so I assume it's my misunderstanding of the HTML5 Web Storage spec, not a browser-specific bug or limitation.

I've tried to make sense of the structured clone algorithm described in http://www.w3.org/TR/html5/infrastructure.html. I don't fully understand what it's saying, but maybe my problem has to do with my object's properties not being enumerable (???)

Is there an easy workaround?


Update: The W3C eventually changed their minds about the structured-clone specification, and decided to change the spec to match the implementations. See https://www.w3.org/Bugs/Public/show_bug.cgi?id=12111. So this question is no longer 100% valid, but the answers still may be of interest.

How to store 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?

Here's my code:

var testObject = { 'one': 1, 'two': 2, 'three': 3 };
console.log('typeof testObject: ' + typeof testObject);
console.log('testObject properties:');
for (var prop in testObject) {
 console.log(' ' + prop + ': ' + testObject[prop]);
}
// Put the object into storage
localStorage.setItem('testObject', testObject);
// Retrieve the object from storage
var retrievedObject = localStorage.getItem('testObject');
console.log('typeof retrievedObject: ' + typeof retrievedObject);
console.log('Value of retrievedObject: ' + retrievedObject);

The console output is

typeof testObject: object
testObject properties:
 one: 1
 two: 2
 three: 3
typeof retrievedObject: string
Value of retrievedObject: [object Object]

It looks to me like the setItem method is converting the input to a string before storing it.

I see this behavior in Safari, Chrome, and Firefox, so I assume it's my misunderstanding of the HTML5 Web Storage specification, not a browser-specific bug or limitation.

I've tried to make sense of the structured clone algorithm described in 2 Common infrastructure . I don't fully understand what it's saying, but maybe my problem has to do with my object's properties not being enumerable (???).

Is there an easy workaround?


Update: The W3C eventually changed their minds about the structured-clone specification, and decided to change the spec to match the implementations. See 12111 – spec for Storage object getItem(key) method does not match implementation behavior . So this question is no longer 100% valid, but the answers still may be of interest.

improve title
Link
Flip
  • 6.9k
  • 8
  • 54
  • 84

Storing How to Store Objects in HTML5 localStorage?

Add link to W3C resolution
Source Link
Kristopher Johnson
  • 82.8k
  • 55
  • 253
  • 310
Loading
Loading
Question Protected by NullPoiиteя
Mod Removes Wiki by casperOne
added 5 characters in body
Source Link
Kristopher Johnson
  • 82.8k
  • 55
  • 253
  • 310
Loading
deleted 11 characters in body
Source Link
Kristopher Johnson
  • 82.8k
  • 55
  • 253
  • 310
Loading
added 71 characters in body
Source Link
Kristopher Johnson
  • 82.8k
  • 55
  • 253
  • 310
Loading
added 6 characters in body; added 11 characters in body
Source Link
Kristopher Johnson
  • 82.8k
  • 55
  • 253
  • 310
Loading
added 86 characters in body
Source Link
Kristopher Johnson
  • 82.8k
  • 55
  • 253
  • 310
Loading
Source Link
Kristopher Johnson
  • 82.8k
  • 55
  • 253
  • 310
Loading
default

AltStyle によって変換されたページ (->オリジナル) /