Preserve a Variable Between Web Pages
If you have web pages with menus or variables or selections and you want to preserve what these are as you navigate amongst your web pages, you need something very special. You could use cookies but they may not be set to be "allowed." If you do use them, try to delete them as your site is exited. If you count on cookies for inter-page navigation, you may need to ask if they have cookies allowed—an embarrassing question reflecting on questionable programming abilities. Especially since there's an alternative. Even though if you have forms where visitors enter names or other data, and you want to be able to remember them the next time they visit, cookies are warranted, the same is not true about using cookies for navigation.
The following method takes a single character representing a choice or variable or menu selection and sends your visitor to one of your other web pages (webpage2.html) from your first web page (webpage1.html), but it remembers the choice/variable/menu selection.
Include any number of selections to choose from on page 1:
<div onClick="javascript:window.location.replace('webpage2.html?t');return true">Choose Top View</div>
<div onClick="javascript:window.location.replace('webpage2.html?c');return true">Choose Close-up View</div>
<div onClick="javascript:window.location.replace('webpage2.html?s');return true">Choose Side View</div>
Call this function from the body tag on page 2, replacing the backgrnd() function with your own:
function fix(){m = location.search.substr(1).split("?");if(m=='t'){backgrnd('b')};if(m=='s'){backgrnd('c')};if(m=='c'){backgrnd('d')}
The body tag:
<body onload='fix()'>
Note that even if the choices you want to preserve are words like "Blue" or phrases like "The Shadow of Your Smile," a single character can do it, since in the second page you can retrieve the whole phrase by just having an array of strings that contain the phrases, and your variable can be the array element number.
Nutty as it sounds, you need to do it differently for more than one variable, due to problems with window.location being interpreted as a property that doesn't like to be parsed as a string, rather than being interpreted as a text string that doesn't mind being parsed as a string. Below is the code, in which adding an empty string does the convert-to-string trick. Split() is a method to break a string into 2 or more array elements. In our case, the first is the url address that we don't need, and the latter element is the data after the "?". Use the second array element, q[1], since it contains the 2 variables. It turns out when you're only messing with one character, you don't have to mess with the arrays. Go figure. So here's the code for getting the variables out of the url address:
x=window.location+"";q = x.split("?");r=q[1];f=r.charAt(0);m=r.charAt(1);
The code for adding the variables to the url address is the same as for the 1-variable method except for the need to choose which values to include according to the current variable's value.
<div onClick="javascript:backgrnd('b');if(f=='m'){window.location.replace('squirrel-valley-railroad.htm?mt')}else{window.location.replace('squirrel-valley-railroad.htm?wt')};return true">Choose Top View</div>
<div onClick="javascript:backgrnd('b');if(f=='m'){window.location.replace('squirrel-valley-railroad.htm?ms')}else{window.location.replace('squirrel-valley-railroad.htm?ws')};return true">Choose Side View</div>
<div onClick="javascript:backgrnd('b');if(f=='m'){window.location.replace('squirrel-valley-railroad.htm?mc')}else{window.location.replace('squirrel-valley-railroad.htm?wc')};return true">Choose Close-up View</div>