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,372
  • 6
  • 36
  • 58

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"];
S. A.
  • 3,714
  • 2
  • 20
  • 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,324
  • 4
  • 27
  • 48