7

Possible Duplicate:
Persist javascript variables across pages?
how to exchange variables between two html pages

I am working on creating a "quiz" that allows users to answer questions, then passes their answer and current score to the next page for the next question. I used this article and was able to get the user's answer to pass through to the next window:

http://www.htmlgoodies.com/beyond/javascript/article.php/3471111/A-Quick-Tutorial-on-JavaScript-Variable-Passing.htm

The issue is that this passes the information through a form. Once I receive the score on the second page, I need to increment the score if the answer was correct. I can't figure out how to pass a javascript variable with an html form.

I feel like this should be a relatively easy fix, I'm just stumped.

Any thoughts or advice would be much appreciated! xoxo

Community
  • 1
  • 1
Rachel Rine
  • 468
  • 1
  • 4
  • 7

2 Answers2

7

There are two obvious ways to maintain state in the browser without requiring that the server remember it between pages:

  1. Cookies

  2. localStorage

The latter is trivial to implement, but is only available in HTML5.

Note that neither is intended to be secure - a determined page hacker could set either to whatever value they wish.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • 1
    One can also use querystrings and hashtags as well. Then the receiving page will read the url and parse them. Both are visible though and are easily editable. – Joseph Jul 20 '12 at 14:46
  • A hashtag is a phase marked as "Please search me" on Twitter and is not to be confused with fragment identifiers on URIs (which are [evil when used for Ajaxy things](http://isolani.co.uk/blog/javascript/BreakingTheWebWithHashBangs)). – Quentin Jul 20 '12 at 15:07
3

You can submit a form using get method <form method="get" then use javascript to parse params in url. Reference here:

Idris
  • 997
  • 2
  • 10
  • 27
James
  • 13,571
  • 6
  • 61
  • 83
  • But this method is not safe, every one can read the source and change things.... You should have server involve in this case – James Jul 20 '12 at 14:48
  • And with Java Script this is more "ugly" to use than just simply using localStorage – tim.baker Jan 08 '13 at 15:25
  • Yeah, Urgly but compartiple to more browser, older browser that does not support local storage – James Jan 08 '13 at 15:28