JavaScript DHTML/Language Basics/If — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 07:24, 26 мая 2010

An if..else if...else statement.

   
<html>
<body>
<script type="text/javascript">
var d = new Date()
var time = d.getHours()
if (time<10){
    document.write("<b>Good morning</b>")
}else if (time>10 & time<16){
    document.write("<b>Good day</b>")
}else{
    document.write("<b>Hello World!</b>")
}
</script>

</body>
</html>



Compare int value

   
<HTML>
<HEAD>
</HEAD>
<BODY>
<SCRIPT language="JavaScript">
<!-- 
    var num1=0;
    var num2=0;
    if(num1==num2) {
        window.alert("True");
    }else {
        window.alert("False");
    }
//-->
</SCRIPT>
</BODY>
</HTML>



Compare string and alert in dialog

   
<HTML>
<HEAD>
<SCRIPT language="JavaScript">
<!-- 
var thesport="Golf";
var myfood="Pizza";
if(thesport=="Football"){
    window.alert("Cool Sport!");
}else{
    window.alert("That sport might be cool.");
}
if(myfood=="Pizza"){
    window.alert("My favorite food!");
}else{
    window.alert("That food sounds OK I guess.");
}
//-->
</SCRIPT>
</HEAD>
<BODY>

</BODY>
</HTML>



Compare string value in if statement

   
<HTML>
<HEAD>
<SCRIPT language="JavaScript">
var thesport="Golf";
if(thesport=="Football")
{
    alert("Yes");
}else{
    alert("No");
}
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>



Deeply Nested if. . .else Constructions

   
<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>



Determining Whether Lowercase or Uppercase Strings Are "More Equal" Using an If Statement

   
<HTML>
<BODY>
<H1>
<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>
</H1>
</BODY>
</HTML>



Example for If...Else statement.

   

<html>
<body>
<script type="text/javascript">
var d = new Date()
var time = d.getHours()
if (time < 10) {
    document.write("<b>Good morning</b>")
}else{
    document.write("<b>Good day</b>")
}
</script>
</body>
</html>



If statement

   

<html>
<body>
<script type="text/javascript">
var d = new Date()
var time = d.getHours()
if (time < 10) {
    document.write("<b>Good morning</b>")
}
</script>
</body>
</html>



if...then...else...if

   

<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>



Is your input a Yes

  
<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>



Nested if statement, logic operator and regular expression

  
<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>



Nested if Statements

   
<html>
<head>
  <title>Nested if Statements</title>
</head>
<body>
  <script type="text/javascript">
  <!--
    var needsInfo;
    var needsMoreInfo;
   
    needsInfo = true;
    needsMoreInfo = true;
   
    if (needsInfo) {
      document.writeln("info true");
    if(needsMoreInfo){
      document.write("<br>needs info true");
    }
    document.writeln("<br>Ordering is easy using our on-line service.");
    }
  // -->
  </script>
</body>
</html>



Nested Indented Conditional statements

   
<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>



Testing value in range

   

<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>



The else Block Responds to a false Value

   
<html>
<head>
  <title>The else Block Responds to a false Value</title>
</head>
<body>
  <script type="text/javascript">
  <!--
    var purchaseAmount;
    purchaseAmount = 101.00;
   
    if(purchaseAmount > 500.00){
      document.write("Thank you for your purchase!");
    }else{
      document.writeln("Thank you, but surely there is something ");
    }
  // -->
  </script>
</body>
</html>



Use if statement to check the value range

  

<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>