1

I have string variable and array in my page. I want to pass these variables to another pages. For string variable yes I can use querystring but for array what can I do?

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
Kadir
  • 3,094
  • 4
  • 37
  • 57

2 Answers2

2

You can store it in localStorage assuming the user has a modern browser. Otherwise you could serialize it, e.g. to JSON, and store it in a cookie.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
2

localStorage can only handle strings which is why you have to first convert your array into a string before storing it.

var yourArray = [ 1, 2, 3 ];

// Store it
localStorage['foo'] = JSON.stringify( yourArray );

// And retrieve it
var storedArray = JSON.parse( localStorage['foo'] );

Like others have said the above only works with modern browsers so if you are worried about browser compatibility you can store your array in a cookie.

If you have concerns regarding size restrictions of cookies and the size of your array check out this question

Community
  • 1
  • 1
Bruno
  • 5,772
  • 1
  • 26
  • 43