0

right now i set something like this below

localStorage.setItem("username", "Smith");

I just want to save different value for "username" in different page..

So is there something likebelow ?

localStorage[page1].setItem("username", "Smith"); 
Vishnu
  • 2,252
  • 6
  • 30
  • 55

2 Answers2

2

You could use a json object with the data associated to a specific page, e.g.

localStorage.setItem("page1", JSON.stringify({username: "Smith"}));
username = JSON.parse(localStorage.getItem("page1"))["username"];
Sergio A.
  • 3,666
  • 2
  • 19
  • 31
0

Local storage is limited to storing key/value pairs. However, you can create a Json object with values for username on different pages, stringify it and store as a key/value pair. check out Storing Objects in HTML5 localStorage

Community
  • 1
  • 1
nuway
  • 2,222
  • 4
  • 26
  • 48