user2057440
user2057440

Reputation: 1

Persist a JavaScript var across 2 pages/steps of a form

So i have a page that effects a javascript var by using a range of dropdown boxes, radio buttons etc. However I need that var to stay the same for that client so when they click add to chart, the var is then followed onto the amount to pay on payment. I'm not sure of the best way to go about it?

Anyone got any pointers to help me?

Cheers

Upvotes: 0

Views: 79

Answers (2)

Ruan Mendes
Ruan Mendes

Reputation: 92334

Another solution is to use http://www.thomasfrank.se/sessionvars.html By setting

sessvars.myObj = {name: "Thomas", age: 35}

You will be able to access sessvars variable from other pages, it's much easier to work than cookies and it has the benefit of not adding cookies to every request to the server. Also cookies can be a pain to encode and decode.

If you only need to support the latest browsers, you should use localStorage.

var foo = localStorage.getItem("bar");
localStorage.setItem("bar", foo);

Upvotes: 1

PurkkaKoodari
PurkkaKoodari

Reputation: 6809

You could use:

  • Server side scripting to print the values directly in the JavaScript.
  • The other page's query string, read using JavaScript. (location.search)
  • The same with the hash string. (location.hash)
  • Cookies, with server-side scripting or directly in JavaScript.
  • Local storage (HTML5).

If the information must be secure, I would use a POST form and server-side scripting. Otherwise I would use cookies or local storage.

Upvotes: 2

Related Questions