JavaScript DHTML/Language Basics/Switch
Содержание
Another switch Statement
<HTML>
<HEAD>
<TITLE>Using the switch Statement</TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript"><!--
for(i=1; i<=10; ++i) {
switch (i) {
case 1:
val = "one"
break;
case 2:
val = "two"
break;
case 3:
val = "three"
break;
case 4:
val = "four"
break;
case 5:
val = "five"
break;
case 6:
val = "six"
break;
case 7:
val = "seven"
break;
case 8:
val = "eight"
break;
case 9:
val = "nine"
break;
case 10:
val = "ten"
break;
default:
val = "unknown"
}
document.writeln(val+"<BR>");
}
// -->
</SCRIPT>
</BODY>
</HTML>
Switch based on integer value
<HTML>
<BODY>
<H1>
<SCRIPT>
var n = 1;
switch (n) {
case 1: {
document.write("n equals 1");
break;
}
case 2:{
document.write("n equals 2");
break;
}
case 3:{
document.write("n equals 3");
break;
}
default:{
document.write("No match!");
break;
}
}
</SCRIPT>
</H1>
</BODY>
</HTML>
Switch statement
/* Note that Sunday=0, Monday=1, Tuesday=2, etc.*/
<html>
<body>
<script type="text/javascript">
var d = new Date()
theDay=d.getDay()
switch (theDay){
case 5:
document.write("Finally Friday")
break
case 6:
document.write("Super Saturday")
break
case 0:
document.write("Sleepy Sunday")
break
default:
document.write("Not weekend!")
}
</script>
</body>
</html>
Switch with string value
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
var inputName = prompt("Please enter your name:");
switch(inputName) {
case "Tom":
document.write("Welcome " + inputName);
break;
case "Name":
document.write("Name");
break;
default:
document.write("default:" + inputName);
}
</script>
</body>
</html>
The switch Construction in Action
/*
JavaScript Bible, Fourth Edition
by Danny Goodman
John Wiley & Sons CopyRight 2001
*/
<HTML>
<HEAD>
<TITLE>Switch Statement and Labeled Break</TITLE>
<SCRIPT LANGUAGE="JavaScript1.2">
// build two product arrays, simulating two database tables
function product(name, price) {
this.name = name
this.price = price
}
var ICs = new Array()
ICs[0] = new product("Septium 900MHz","$149")
ICs[1] = new product("Septium Pro 1.0GHz","$249")
ICs[2] = new product("Octium BFD 750MHz","$329")
var snacks = new Array
snacks[0] = new product("Rays Potato Chips","$1.79")
snacks[1] = new product("Cheezey-ettes","$1.59")
snacks[2] = new product("Tortilla Flats","$2.29")
// lookup in the "table" associated with the product
function getPrice(selector) {
var chipName = selector.options[selector.selectedIndex].text
var outField = document.forms[0].cost
master:
switch(selector.options[selector.selectedIndex].value) {
case "ICs":
for (var i = 0; i < ICs.length; i++) {
if (ICs[i].name == chipName) {
outField.value = ICs[i].price
break master
}
}
break
case "snacks":
for (var i = 0; i < snacks.length; i++) {
if (snacks[i].name == chipName) {
outField.value = snacks[i].price
break master
}
}
break
default:
outField.value = "Not Found"
}
}
</SCRIPT>
</HEAD>
<BODY>
<B>Branching with the switch Statement</B>
<HR>
Select a chip for lookup in the chip price tables:<P>
<FORM>
Chip:<SELECT NAME="chips" onChange="getPrice(this)">
<OPTION>
<OPTION VALUE="ICs">Septium 900MHz
<OPTION VALUE="ICs">Septium Pro 1.0GHz
<OPTION VALUE="ICs">Octium BFD 750MHz
<OPTION VALUE="snacks">Rays Potato Chips
<OPTION VALUE="snacks">Cheezey-ettes
<OPTION VALUE="snacks">Tortilla Flats
<OPTION>Poker Chipset
</SELECT>
Price:<INPUT TYPE="text" NAME="cost" SIZE=10>
</FORM>
</BODY>
</HTML>
The switch Statement
<html>
<head>
<title>The switch Statement</title>
</head>
<body>
<script language="JavaScript1.2" type="text/javascript">
<!--
var request = "Name";
switch(request){
case "Logo" :
document.write("<img src="logo.gif" alt="Logo">");
document.write("<br>");
break;
case "Name" :
document.write("Software Inc.");
document.write("<br>");
break;
case "Products" :
document.write("MyEditor");
document.write("<br>");
break;
default :
document.write("www.wbex.ru");
break;
}
//-->
</script>
</body>
</html>
Use switch with true false value
<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
var yourchoice;
yourchoice = confirm("True of false")
switch (yourchoice)
{
case true:
alert("true!");
break;
case false:
alert("false");
break;
}
</script>
</head>
<body>
</body>
</html>