[thelist] javascript variable issue (continued)

Hassan Schroeder hassan at webtuitive.com
Fri Aug 31 09:37:55 CDT 2001


ALBIE ATTIAS wrote:

> I made the changes you suggested but I'm now getting a javascript error on
> page 2 saying "documents.forms.quiz.q1 is not an object". What am I doing
> wrong?

Your script's reference to the form name and the form name itself
don't match --

/* from script */
	eval("document.forms['quiz']." + argname + ".value=" + value + ";");
 
<!-- in body -->
	<form name="page1answers" method="get" action="page3.htm">

Also, the values aren't being set, so 

/* old */
       var value = pairs[i].substring(pos+1);    // Extract the value
       value = unescape(value);                  // Store as a decoded value
       eval("document.forms['quiz']." + argname + ".value=" + value + ";");
  
/* new - changing "value" to a non-reserved word :-) */

	var thisValue = pairs[i].substring(pos+1); // Extract the value

	thisValue = unescape(thisValue);           // Store as a decoded value
	document.forms['quiz'].elements[argname].value = thisValue;

> Also, assuming everything else is ok, what line(s) of code do I use on page3
> to access my stored answers?

Example: move the function code out of the head and put it where you want
to display it --

	<!-- within the body -->
<p>Your answers were as follows:<p>

<script language="javascript">

   var query = location.search.substring(1);     // Get query string

   var pairs = query.split("&");                 // Split at ampersand

   for (var i=0; i < pairs.length; i++)
	{
	var pos = pairs[i].indexOf('=');          // Look for "name=value"

	if (pos == -1) continue;                  // If not found, skip

	var argname = pairs[i].substring(0,pos);  // Extract the name

	var value = pairs[i].substring(pos+1);    // Extract the value

	value = unescape(value);          	 // Store as a decoded value
	document.write(argname + " = " + value + "<br />");
	}
</script>

I'll leave the grading part to you :-) 

HTH!
-- 
H*
Hassan Schroeder ----------------------------- hassan at webtuitive.com 
Webtuitive Design ===  (+1) 408-938-0567   === http://webtuitive.com

    -- creating dynamic Web sites and applications since 1994 --




More information about the thelist mailing list