ITP
ITP

Reputation: 7

Using localstorage

Got myself in a tricky situation. I'm using local storage to save values from a popup window, and then paste them into an input when focus returns to parent window.

But then something rather awkward takes place, when I try to store ';' separated values, is that I get only the 1st set, losing all the rest of the string. What makes it more bizarre is that after saving my value, I test by calling

alert('SELECTED : ' +   localStorage.getItem('MyStr'));

the whole string is there... but on the script I retrieve this value, when i'm checking

alert(localStorage.getItem('MyStr'));

Only the 3rd set is there, i.e.: I store something like

abcdefg;123323;ffasfs;5445;iuiuifa;

but when I need to get it back, theres only

ffasfs

I could use some help then, I'm all new to this whole thing, and killing myself to get a website working. Thanks in advance, sorry if my question looks stupid.

Upvotes: 0

Views: 149

Answers (1)

Erik5388
Erik5388

Reputation: 2211

Store your values in localStorage as JSON strings. This may even help you build more complex objects for the future.

For now though... Just do:

localStorage.setItem("your key", JSON.stringify("abcdef;1234;whatever"));

This procedure will not only sanitize your input but also create oppertunity to store serialized objects in the future.

It's important to note that while JSON.stringify is pretty much supported everywhere... Not all browsers have it built in.

For those cases, check out json2.js.

Hope that helps.

Upvotes: 1

Related Questions