Reuben Ward
Reuben Ward

Reputation: 99

Regarding javascript variables

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?

Upvotes: -1

Views: 47

Answers (2)

Jakob
Jakob

Reputation: 24360

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.

Upvotes: 4

murli2308
murli2308

Reputation: 2994

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;

Upvotes: 0

Related Questions