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?

1
  • you can use local storage (indexDB) if mordern browser. else go for cookies if data is not too large. Commented Dec 10, 2012 at 12:45

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.