Simple if if ... else Multiple if ... else Conditions Compound if (AND and OR) Nested if |
Start of website Alphabetical index |
<HTML> <HEAD> <SCRIPT TYPE="TEXT/JAVASCRIPT"> // Check dates function check() { if (document.date.day.value > 31) { alert ("Day number too large") return } if (document.date.month.value > 12) { alert ("Month number too large") return } alert ("Date is OK") } </SCRIPT> </HEAD> <BODY> <FORM ACTION="#" NAME=date> Enter day and month<BR> <INPUT TYPE=TEXT NAME=day SIZE=2> <INPUT TYPE=TEXT NAME=month SIZE=2> <INPUT TYPE=BUTTON VALUE="Check date" ONCLICK="check()"> </FORM> </BODY> </HTML> |
There are JavaScript value of false and true. You can avoid using them, but they are occasionally helpful, when you get on to functions and more complicated coding. So you could code "x=true; if (x) {...}
<HTML> <HEAD> <SCRIPT TYPE="TEXT/JAVASCRIPT"> // Put input numbers into order function order() { in1 = parseFloat(document.ord.ina.value) in2 = parseFloat(document.ord.inb.value) if (in1 < in2) { out1 = in1 out2 = in2 } else { out1 = in2 out2 = in1 } document.ord.outa.value = out1 document.ord.outb.value = out2 } </SCRIPT> </HEAD> <BODY> <FORM ACTION="#" NAME=ord> Enter two numbers<BR><BR> <INPUT TYPE=TEXT NAME=ina SIZE=5> <INPUT TYPE=TEXT NAME=inb SIZE=5> <BR><BR> <INPUT TYPE=BUTTON VALUE="Output in order" ONCLICK="order()"> <BR><BR> <INPUT TYPE=TEXT NAME=outa SIZE=5 DISABLED> <INPUT TYPE=TEXT NAME=outb SIZE=5 DISABLED> </FORM> </BODY> </HTML> |
*** Hello World! *** |
<HTML> <HEAD> <SCRIPT TYPE="TEXT/JAVASCRIPT"> // Make text different colours function change() { var str = "" if (document.col.shade.value == "red") {str = "FF0000"} else if (document.col.shade.value == "green") {str = "00FF00"} else if (document.col.shade.value == "blue") {str = "0000FF"} document.getElementById('fontcol').innerHTML="<FONT COLOR='#" + str + "'>*** Hello World! ***<" + "/FONT>" } </SCRIPT> </HEAD> <BODY> <FORM ACTION="#" NAME=col> <SELECT NAME="shade" ONCHANGE="change()"> <OPTION VALUE="red">red</OPTION> <OPTION VALUE="green">green</OPTION> <OPTION VALUE="blue">blue</OPTION> </SELECT> </FORM> <SPAN ID="fontcol"> <FONT COLOR="#FF0000">*** Hello World! ***</FONT> </SPAN> </BODY> </HTML> |
Condition | JavaScript | a contains | ||
---|---|---|---|---|
3 | 5 | 7 | ||
equals | if (a == 5) {...} | False | True | False |
less than | if (a < 5) {...} | True | False | False |
greater than | if (a > 5) {...} | False | False | True |
not equals | if (a != 5) {...} | True | False | True |
less than | if (a <= 5) {...} | True | True | False |
greater than | if (a >= 5) {...} | False | True | True |
<HTML> <HEAD> </HEAD> <BODY> <SCRIPT TYPE="TEXT/JAVASCRIPT"> var hour=new Date().getHours() var greet="" if (hour<=6) {greet="Good night!"} if (hour>6 && hour<=12) {greet="Good morning!"} if (hour>12 && hour<=18) {greet="Good afternoon!"} if (hour>18 && hour<=24) {greet="Good evening!"} document.write (greet) document.write ("<BR><BR>") if (greet=="Good morning!" || greet=="Good afternoon!") {document.write ("It's day-time!")} if (greet=="Good evening!" || greet=="Good night!") {document.write ("It's night-time!")} </SCRIPT> </BODY> </HTML> |
<HTML> <HEAD> </HEAD> <BODY> <SCRIPT TYPE="TEXT/JAVASCRIPT"> var hour=new Date().getHours() var greet="" if (hour<=6) {greet="Good night!"} if (hour>6) { if (hour<=12) {greet="Good morning!"} } if (hour>12) { if (hour<=18) {greet="Good afternoon!"} } if (hour>18) { if (hour<=24) {greet="Good evening!"} } document.write (greet) document.write ("<BR><BR>") if (greet=="Good morning!") {document.write ("It's day-time!")} if (greet=="Good afternoon!") {document.write ("It's day-time!")} if (greet=="Good evening!" ) {document.write ("It's night-time!")} if (greet=="Good night!") {document.write ("It's night-time!")} </SCRIPT> </BODY> </HTML> |
© Jo Edkins 2005