...

View Full Version : AndyB



AndyB
07-10-2002, 10:52 PM
Something (or some things) are wrong with this. The purpose should be clear, but my code doesn't work. Anyone want to point out theobvious mistake(s)??

<html>
<head>

<script type="text/javascript">
function goToIt(){
if (document.myform.exper.value>="10") {window.location="http://whatever.com/oldenuf.html"}
return true;
}
else;
{window.location="http://whatever.com/nogood.html"}
return true;
}
</script>

</head>
<body>
<form name="myform" onSubmit="return goToIt()" method="get">
How many years experience do you have:&nbsp;&nbsp;<input type="text" name="exper" size="8">
<input type="submit" name="submit" value="Press to Continue">
</form>
</body>
</html>

adios
07-10-2002, 11:25 PM
function goToIt(){
if (document.myform.exper.value>="10")
you're comparing strings, not numbers
{window.location="http://whatever.com/oldenuf.html"}
return true;
why return anything? you've just changed pages
}
else;
semicolon after else statement
{window.location="http://whatever.com/nogood.html"}
return true;
ditto
}

Might as well filter the input:

<html>
<head>
<script type="text/javascript">

function goToIt(field) {
if (/^\d+$/.test(field.value)) { //number entered?
field.form.action = (Number(field.value)>=10) ? //convert entry to number,set form action
'http://whatever.com/oldenuf.html' : 'http://whatever.com/nogood.html';
return true; //submit
}
alert('Please enter a number.'); //prompt,focus,cancel
field.focus();
field.select();
return false;
}

</script>

</head>
<body>
<form name="myform" onsubmit="return goToIt(exper)">
How many years experience do you have: <input type="text" name="exper" size="8">
<input type="submit" value="Press to Continue">
</form>
</body>
</html>

ConfusedOfLife
07-10-2002, 11:28 PM
well, first of all you should write :

if (document.myform...... >= 10 ) instead of if(docu..... >= "10")

You shouldn't put that ; after else
and it's no need to use an "else" clause at all! coz if the condition of if is right, then the return true works and you'll be out of your function, no need to put that else. now I put the correct program that really works here!!


<html>
<head>

<script type="text/javascript">
function goToIt()
{
if (document.myform.exper.value >= 10)
{
window.location="http://whatever.com/oldenuf.html"
return true;
}
window.location="http://whatever.com/nogood.html"
return true;
}
</script>

</head>
<body>
<form name="myform" method="get">
How many years experience do you have:
<input type="text" name="exper" size="8">
<input type="button" name="submit" value="Press to Continue" onclick="goToIt();">
</form>
</body>
</html>

ASAAKI
07-10-2002, 11:42 PM
the function should be:

<script type="text/javascript">
function goToIt(){
if (document.myform.exper.value>="10") {window.location="http://whatever.com/oldenuf.html";
return true;
}
else
{window.location="http://whatever.com/nogood.html" ;
return true; }
}
</script>


i don't know about the return true and false parts, i didn't bother to try to get the function in the first place. i just know this:

the if-else syntax is
if(condition){
whatever statement...;
}--> you shouldn't have put 'return true;' after the curlies-both times
else{
whatever...;
}

you closed the curly brackets too soon in both cases, and the return trues were cruelly excluded from the curly brackets of the 'if' and 'else' clauses they so truly deserved to be in :eek:
:p

jkd
07-11-2002, 01:13 AM
FYI there is nothing wrong when you compare a number against a string, as long as the string is nothing but digits:

10 > '5' // true
10 > '5px' // false

But it only will typecast one operand:

'10' > '5' // false

because both operands are compared as strings.

This was causing a problem here:

document.myform.exper.value >= "10"

Both are strings. If you did either:

Number(document.myform.exper.value) >= "10"

or

document.myform.exper.value >= 10

it would have been fine. You should explicitly typecast though just to be clear and safe:

Number(document.myform.exper.value) >= 10

There was also that semicolon problem with the else statement someone else pointed out.


You could shorten your entire code with the use of the ternary operator as well:

window.location.href = (parseInt(document.myform.exper.value) >= 10) ? 'http://whatever.com/oldenuf.html' : 'http://whatever.com/nogood.html';

And throw the return true after that if you must, or you could just get plain silly and utilize awful coding habits for fun:

return Boolean(window.location.href = (parseInt(document.myform.exper.value) >= 10) ? 'http://whatever.com/oldenuf.html' : 'http://whatever.com/nogood.html');

Not recommended though ;).

AndyB
07-11-2002, 01:41 AM
Thanks all. Assistance appreciated.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum