4

I want to store a JavaScript object in HTML5 local storage.

sessionStorage.setItem("localObj",this);

I am using the above statement within a function.The object contains HTML tags. I can neither convert it to a string nor to a JSON. How do i proceed with this?

2
  • 1
    > I can neither convert it to a string nor to a JSON. Why? you can convert an HTMLElement (with it's children) to a string and convert it back when needed. Commented Aug 27, 2013 at 7:16
  • you are getting error 'Converting circular structure to JSON' because when you are making json structure of your dom element then it create circular dependancies. like you have a property inside your dom called 'parentNode', 'parentElement','offsetParent' etc which have your dom element as their child. So it create circular dependancy and json cant be serialize because of that only. Commented Aug 27, 2013 at 11:05

3 Answers 3

8

You have to first convert the object into json and then store in local storage. And when you want to reuse the stored data you have to deserialize the json string into javascript object and it will work fine.

Working Sample

function setValue() {
    var obj = new user();
    var jsonObject = JSON.stringify(obj);
    sessionStorage.setItem("Gupta", jsonObject);
    getValue();
}

function user() {
    this.Name = "rahul";
    this.Age = 20;       
}

function getValue() {
    var json_string = sessionStorage.getItem("Gupta");
    var obj = JSON.parse(json_string)
    alert("Name = "+obj.Name + ", Age = " + obj.Age);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Local storage will only store strings. If you can't make a string representation of your object, then you can't store it.

Comments

0

Local storage is designed with string / value pairs in mind, so you'll have a hard time trying to store your object 'as is'. You either need to 'stringify' it, or you need to look at something else. The answers to this earlier question should assist:

Storing Objects in HTML5 localStorage

3 Comments

The javascript object contains HTML and is like below <a title="" class="" style="" active=""> <img src = ""/> </a> If i use JSON.stringfy(obj) i get error : Converting circular structure to JSON
Understood. One approach would be to implement your own toJSON() function within the Javascript object. This could then handle the HTML entities therein as required. As soon as stringify encounters your object, it will defer to your object's implementation of toJSON().
you are getting error 'Converting circular structure to JSON' because when you are making json structure of your dom element then it create circular dependancies. like you have a property inside your dom called 'parentNode', 'parentElement','offsetParent' etc which have your dom element as their child. So it create circular dependancy and json cant be serialize because of that only.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.