Click to See Complete Forum and Search --> : onClick Function Problems


Novo_MW
02-08-2004, 11:37 AM
hi
the code below should jump the browser to a new webpage on the click of a button. which website is decided by which radio button is selected.

the code produces no errors and display's correctly, but when the button is pressed nothing happens!

below is an extract of the code minus the html, ive attached a version with the html should it help

your help would be greatly appreachiated


<SCRIPT LANGUAGE="JavaScript">
var flag=0;
function link()
{
if(flag = 0){
window.open('http://www.webpage1.com','_self');
}
else if(flag = 1){
window.open('http://www.webpage2.com','_self');
}
}
</SCRIPT>

<input type=radio name='enable' value='agree' checked onClick=flag=0;>Yes

<input type=radio name='enable' value='disagree' onClick=flag=1;>No

<input type=button name ='button' value="Done!" onClick="link();">

Exuro
02-08-2004, 11:47 AM
It would seem that you've made the most common error in programming...


<SCRIPT LANGUAGE="JavaScript">
var flag=0;
function link()
{
if(flag == 0){
window.open('http://www.webpage1.com','_self');
}
else if(flag == 1){
window.open('http://www.webpage2.com','_self');
}
}
</SCRIPT>


The = (singe equal sign) is the assignment operator. This sets the variable on the left equal to that on the right, and it almost always returns true. The == (double equal sign) is the equality operator, and it compares the values on the right and left. You wanted the equality operator, not the assignment operator. Secondly, your code in the HTML file looks like this:


window.open=('http://www.webpage2.com','_self');


You are not supposed to have that equal sign there...

Novo_MW
02-08-2004, 01:24 PM
thank you
a complete and utter school boy error on my part