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?
Asked
Active
Viewed 304 times
1
-
you can use local storage (indexDB) if mordern browser. else go for cookies if data is not too large. – Ashish Kasma Dec 10 '12 at 12:45
2 Answers
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