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?
2 Answers
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
1 Comment
Kadir
I'm using this method now and also storing the cookie. Thanks for help.