JavaScript DHTML/Language Basics/If
Содержание
- 1 An if..else if...else statement.
- 2 Compare int value
- 3 Compare string and alert in dialog
- 4 Compare string value in if statement
- 5 Deeply Nested if. . .else Constructions
- 6 Determining Whether Lowercase or Uppercase Strings Are "More Equal" Using an If Statement
- 7 <SCRIPT>
if ("aa" > "AA") {
document.write("Lower case is greater than upper case!");
} else if ("hogwarts" == "HOGWARTS") {
document.write("They are the same!");
} else {
document.write("Upper case is greater than lower case!");
}
</SCRIPT>
- 7.1 Example for If...Else statement.
- 7.2 If statement
- 7.3 if...then...else...if
- 7.4 Is your input a Yes
- 7.5 Nested if statement, logic operator and regular expression
- 7.6 Nested if Statements
- 7.7 Nested Indented Conditional statements
- 7.8 Testing value in range
- 7.9 The else Block Responds to a false Value
- 7.10 Use if statement to check the value range
An if..else if...else statement.
<source lang="html4strict">
<html> <body> <script type="text/javascript"> var d = new Date() var time = d.getHours() if (time<10){
document.write("Good morning")
}else if (time>10 & time<16){
document.write("Good day")
}else{
document.write("Hello World!")
} </script>
</body> </html>
</source>
Compare int value
<source lang="html4strict">
<HTML> <HEAD> </HEAD> <BODY> <SCRIPT language="JavaScript">
</SCRIPT> </BODY> </HTML>
</source>
Compare string and alert in dialog
<source lang="html4strict">
<HTML> <HEAD> <SCRIPT language="JavaScript">
</SCRIPT> </HEAD> <BODY>
</BODY> </HTML>
</source>
Compare string value in if statement
<source lang="html4strict">
<HTML> <HEAD> <SCRIPT language="JavaScript"> var thesport="Golf"; if(thesport=="Football") {
alert("Yes");
}else{
alert("No");
} </SCRIPT> </HEAD> <BODY> </BODY> </HTML>
</source>
Deeply Nested if. . .else Constructions
<source lang="html4strict">
<HTML> <HEAD> <SCRIPT LANGUAGE="JavaScript"> function testLetter(form){
inpVal = form.entry.value; if (inpVal != "") { if (inpVal == "A") { alert("Thanks for the A."); } else if (inpVal == "B") { alert("Thanks for the B."); } else if (inpVal == "C") { alert("Thanks for the C."); } else { alert("Sorry, wrong letter or case.") } } else { alert("You did not enter anything.") }
} </SCRIPT> </HEAD> <BODY> <FORM> Please enter A, B, or C and press Enter key: <INPUT TYPE="text" NAME="entry" onChange="testLetter(this.form)"> </FORM> </BODY> </HTML>
</source>
Determining Whether Lowercase or Uppercase Strings Are "More Equal" Using an If Statement
<source lang="html4strict">
<HTML> <BODY>
<SCRIPT> if ("aa" > "AA") { document.write("Lower case is greater than upper case!"); } else if ("hogwarts" == "HOGWARTS") { document.write("They are the same!"); } else { document.write("Upper case is greater than lower case!"); } </SCRIPT>
</BODY> </HTML>
</source>
Example for If...Else statement.
<source lang="html4strict">
<html> <body> <script type="text/javascript"> var d = new Date() var time = d.getHours() if (time < 10) {
document.write("Good morning")
}else{
document.write("Good day")
} </script> </body> </html>
</source>
If statement
<source lang="html4strict">
<html> <body> <script type="text/javascript"> var d = new Date() var time = d.getHours() if (time < 10) {
document.write("Good morning")
} </script> </body> </html>
</source>
if...then...else...if
<source lang="html4strict">
<html> <head> <title>if...then...else...if</title> <script type="text/javascript"> var stateCode = "MO"; if (stateCode == "OR") {
taxPercentage = 3.5;
} else if (stateCode == "CA") {
taxPercentage = 5.0;
} else if (stateCode == "MO") {
taxPercentage = 1.0;
} else {
taxPercentage = 2.0;
} document.write(taxPercentage); </script> </head> <body> </body> </html>
</source>
Is your input a Yes
<source lang="html4strict">
<html> <head>
<title>Confirming Something</title> <script type = "text/javascript"> function processConfirm(answer) { var result = ""; if (answer) { result = "Yes"; } else { result = "No"; } return result; } </script>
</head> <body> <script type = "text/javascript"> var confirmAnswer = confirm("Yes or no?"); var theAnswer = processConfirm(confirmAnswer); document.write(theAnswer); </script> </body> </html>
</source>
Nested if statement, logic operator and regular expression
<source lang="html4strict">
<html> <head>
<title></title>
</head> <body> <script type = "text/javascript" > var inputNum = prompt("Please enter a number between 50 and 100:"); if (isNaN(inputNum)) {
if (inputNum.match(/one|two|three|four|five|six|seven|eight|nine|ten/)) { document.write("it is in English"); } else { document.write(" not a number."); }
} else if ((inputNum > 99) || (inputNum < 51)) {
document.write(inputNum + ", is not between 50 and 100.");
} </script> </body> </html>
</source>
Nested if Statements
<source lang="html4strict">
<html> <head>
<title>Nested if Statements</title>
</head> <body>
<script type="text/javascript"> </script>
</body> </html>
</source>
Nested Indented Conditional statements
<source lang="html4strict">
<html> <head> <title>Nested Indented Conditional statements</title> <script type="text/javascript"> var prefChoice = 1; var stateChoice = "R"; var genderChoice = "F"; if (prefChoice == 1) {
if (stateChoice == "R") { if (genderChoice == "M") { document.write("..."); } }
} </script> </head> <body> </body> </html>
</source>
Testing value in range
<source lang="html4strict">
<html> <head> <title>Testing value in range</title> <script type="text/javascript"> var nValue = 0; if (nValue >= 0 && nValue <= 100) {
alert("value between 0 and 100, inclusive");
} else if (nValue > 0 && nValue < 100) {
alert("value between 0 and 100 exclusive");
} else if (nValue > 100) {
alert ("value over 100");
} else if (nValue > 0) {
alert ("value is negative");
} </script> </head> <body> </body> </html>
</source>
The else Block Responds to a false Value
<source lang="html4strict">
<html> <head>
<title>The else Block Responds to a false Value</title>
</head> <body>
<script type="text/javascript"> </script>
</body> </html>
</source>
Use if statement to check the value range
<source lang="html4strict">
<html> <head> <title></title> </head> <body> <script type="text/javascript"> var temp = prompt("number value:"); if (temp > 100) {
document.write("100");
} else if (temp < 20) {
document.write("20");
} </script> </body> </html>
</source>