Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-10-2002, 09:52 PM   PM User | #1
AndyB
New to the CF scene

 
Join Date: Jun 2002
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
AndyB is an unknown quantity at this point
AndyB

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>
AndyB is offline   Reply With Quote
Old 07-10-2002, 10:25 PM   PM User | #2
adios
Senior Coder

 
Join Date: Jun 2002
Posts: 1,404
Thanks: 2
Thanked 32 Times in 32 Posts
adios is on a distinguished road
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>

Last edited by adios; 07-11-2002 at 01:43 AM..
adios is offline   Reply With Quote
Old 07-10-2002, 10:28 PM   PM User | #3
ConfusedOfLife
Regular Coder

 
Join Date: Jul 2002
Location: Iran
Posts: 695
Thanks: 0
Thanked 0 Times in 0 Posts
ConfusedOfLife is an unknown quantity at this point
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>
ConfusedOfLife is offline   Reply With Quote
Old 07-10-2002, 10:42 PM   PM User | #4
ASAAKI
Regular Coder

 
Join Date: Jul 2002
Location: This little Earth.
Posts: 383
Thanks: 0
Thanked 0 Times in 0 Posts
ASAAKI is an unknown quantity at this point
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
ASAAKI is offline   Reply With Quote
Old 07-11-2002, 12:13 AM   PM User | #5
jkd
Senior Coder

 
jkd's Avatar
 
Join Date: May 2002
Location: metro DC
Posts: 3,163
Thanks: 1
Thanked 18 Times in 18 Posts
jkd will become famous soon enough
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 .
__________________
jasonkarldavis.com
jkd is offline   Reply With Quote
Old 07-11-2002, 12:41 AM   PM User | #6
AndyB
New to the CF scene

 
Join Date: Jun 2002
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
AndyB is an unknown quantity at this point
Thanks all. Assistance appreciated.
AndyB is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:33 AM.


Advertisement
Log in to turn off these ads.