Previous Example-|-Next Chapter's Examples-|-Return to Chapter Listing

Example 6.22:
Using the switch Statement


[This example works with Netscape Navigator 4. It will not work with Netscape Navigator 3 or Internet Explorer 3.]

Click on the button and type something to run the switch.


In the HEAD we have this script: <script language="JAVASCRIPT"> <!-- function runSwitch() { var sColor = prompt("Type Red, White, Green, or Blue (or type something else)") switch (sColor) { case "Red" : alert("Your Favorite Color is Red"); break; case "White" : alert("Your Favorite Color is White"); break; case "Green" : alert("Your Favorite Color is Green"); break; case "Blue" : alert("Your Favorite Color is Blue"); break; default : alert("You didn't type one of the four colors."); } alert("That's all, folks."); } //--> </script> In the BODY of the page we have this button: <form> <input type="button" value=" What's Your Favorite Color? " onclick="runSwitch()"> <P> </form>
Previous Example-|-Next Chapter's Examples-|-Return to Chapter Listing