Before we get into the nitty gritties of how data can be made to persist between pages, I think it would be a good idea to give a concrete example of why one would want to implement such a thing. As a practical example, imagine you’re writing a Web site for your small rock band. You haven’t made it big yet, but you have a cult following that would like to be able to receive notices of your upcoming gigs. On your Web site, you want to create a form for people who would like to sign up to your mailing list, and you want people to be able to confirm their e-mail address before sending it off to you.
Now, if you were a big time operation, you would probably accomplish this by having the form submit it’s contents to a Common Gateway Interface (CGI) program installed on your Web server. The CGI program would take the information submitted (the e-mail address) and dynamically create a confirmation page containing the e-mail address the user entered. This page would be designed to resubmit the information to a second CGI program (this one responsible for adding people to the mailing list) if the user confirmed that the displayed e-mail address was correct.
Unfortunately you’re just a small-time garage band, and you can’t afford Web hosting beyond the free 5MB of Web space provided by your Internet Service Provider, which maintains a strict policy of “no custom CGI scripts”. Besides that, you really don’t have the time it takes to learn how to develop the two CGI programs required.
Lucky for you, there is another way. There are several techniques available to you that require no special support from your Web server, and still allow you to use the information entered into the form on the first page to create the content for the second page. Two such techniques will be presented in this article, along with practical examples of their use. At the heart of each of these techniques lies the JavaScript variable.
JavaScript allows us to create variables in a page. These are named, invisible placeholders where information, such as numbers, pieces of text, or even images can be stored. Often, variables are used to store information that will be used to change the appearance of a Web page on the fly. For example, if you’re creating a mouseover effect for some image on your page, you might use a variable to store the image that will appear when the mouse is over the image.
Creating a variable in JavaScript is simple. The code takes the following form:
var myVariable = storedValue;
This is just the mouse over way of saying the following:
Create a variable called myVariable, and let its value equal storedValue.
Once a value has been assigned to a variable, any reference to the name of that variable in your code will effectively be replaced by the value you have assigned it.
For the purposes of this article, we’ll want to use variables to store information the user has provided. We’ll then use that information to control content the user views. Often, such information is entered by means of a form.
As a simple example, imagine the user has typed some text into an entry field on our Web page, and we wanted to store that text in a variable for later use. The code for the form would be something like the following:
Notice that we have called our form “myForm”, and our entry field “textToStore”. Also, notice the code onSubmit=”return false;” in the