-1

So I am working on a shopping cart that does not use cookies.

In my first page (the store) I define several variables such as item quantities,item names,etc.

In my second page (the checkout) I need to access the variables from the first page and display them to the user. How can I do so?

1

2 Answers 2

4

There is no way to do it in JavaScript without using the environment outside of the language itself. All variables will lose their values when the page reloads.

There are plenty of possible solutions though. Which one is best depends on your constraints:

  • Store them on the server (using ajax or similar).
  • Store them in local storage
  • Store them in a cookie
  • Avoid reloading the page (e.g. single page approach)
  • Put them in the URL and have the new page parse them from there.

Edit: recommendation

The easiest of the above would be to go with the local storage solution, as described by murli2308 below. Write a variable with localStorage.setItem("myVarName", "myVarValue") on the first page and read it with localStorage.getItem("myVarName") on the second page.

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

3 Comments

Thanks for the quick answer. What one of these would you recommend personally?
The main problems with localStorage are 1) lack of support in IE 7, see caniuse.com/#search=localstorage 2) being per-domain, so if your page is under someone else’s domain, you would be sharing localStorage data with others under that domain.
@JukkaK.Korpela: Absolutely, good point! When I said the "easiest" I meant just that; it's very easy to get started with it. Other alternatives above have other benefits and other disadvantages.
0

You can use local storage of HTML5 to store the data.

 localStorage.setItem("key", "value");

Also if you want to use session storage you can use it

 sessionStorage.key = valuetoupdate;

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.