Click to See Complete Forum and Search --> : Why does this not work with netscape?


jc021179
11-06-2003, 05:15 PM
right guys I am using the code below to get a value from a radio box and post it to the window location. It works perfectly with IE but not netscape.

Any Ideas?

<script>
function sorturl() {
for(i=0; i<question1.q1.length; i++)
if(question1.q1[i].checked)
thisans = question1.q1[i].value
window.location = "p2.htm" + "?x" + thisans
}
</script>

<input type="button" onclick="sorturl()" value="Next Question">

gil davis
11-06-2003, 05:20 PM
Netscape (and W3 reccommendations, BTW) requires that a form element be a child of a <FORM> tag. IE just allows you to be non-compliant.

jc021179
11-06-2003, 05:24 PM
yeah sorry it is part of a form.

gil davis
11-06-2003, 08:12 PM
So it would be nice if you would post a link, or zip the file and attach it to your next post.

That way you will have fewer reasons to be sorry. ;)

jc021179
11-07-2003, 03:34 AM
<html>
<head>
<title>Tutorial - Question One</title>
<script>
function sorturl() {
for(i=0; i<question1.q1.length; i++)
if(question1.q1[i].checked)
thisans = question1.q1[i].value
window.location = "p2.htm" + "?x" + thisans
}
</script>
</head>

<body>
<center><h2><u>Introduction to Scripting - Tutorial 1</u></h2></center>
<h4>QUESTION 1</h4>

<form name="question1">

<table align="center" border="2" width="600">
<tr>
<td width="110"><strong>Question 1</strong></td>
<td colspan="3">A proven approach to creating web pages is based on the:</td>
</tr>
<tr bgcolor="cornsilk" valign="top">
<td>Option 1</td>
<td width="450">
Playdo model
</td>
<td><input type="radio" name="q1" value="0" onclick="0"></td>
</tr>
<tr bgcolor="cornsilk" valign="top">
<td>Option 2</td>
<td width="450">
spiral model
</td>
<td><input type="radio" name="q1" value="1" onclick="0"></td>
</tr>
<tr bgcolor="cornsilk" valign="top">
<td>Option 3</td>
<td width="450">
hackers model, if any
</td>
<td><input type="radio" name="q1" value="2" onclick="0"></td>
</tr>
</table>
<br>
<center><input type="button" onclick="sorturl()" value="Next Question">
</form>
</body>
</html>

Gollum
11-07-2003, 04:06 AM
There are two problems in your script...

The first (and this is the one irritating Netscape) is that it is saying question1 is undefined. And sure enough, if you look at the page, question1 is not a property of the window. The form question1 is a property of the document, so you need to change all your references to question1 to document.question1

The second (and IE has a problem with this too) is that if the user doesn't click any of the radio buttons, then the statement:

thisans = document.question1.q1[i].value

will never be executed and so the variable thisans will not be defined when you try to build the next page's URL...

window.location = "p2.htm" + "?x" + thisans

You should probably test whether testans == undefined before moving to the next page.

jc021179
11-07-2003, 05:00 AM
great. got it working now.

thanks

JC