Saturday 25 January 2014

example program for throw keyword

<!DOCTYPE html>
<html>
<body>

<script>
function myFunction()
{
var y=document.getElementById("mess");
y.innerHTML="";
//try
//{
var x=document.getElementById("demo").value;
if(x=="")    throw "empty";
if(isNaN(x)) throw "not a number";
if(x>10)     throw "too high";
if(x<5)      throw "too low";
//}
//catch(err)
//{
y.innerHTML="Error: " + err + ".";
//}
}
</script>

<h1>My First JavaScript</h1>
<p>Please input a number between 5 and 10:</p>
<input id="demo" type="text">
<button type="button" onclick="myFunction()">Test Input</button>
<p id="mess"></p>

</body>


</html>


Example 2:
<!DOCTYPE html>
<html>
<body>

<script>
function myFunction()
{
var y=document.getElementById("mess");
y.innerHTML="";

var x=document.getElementById("demo").value;
if(x=="")   document.writeln( "empty");
if(isNaN(x)) document.writeln("not a number");
if(x>10)   document.writeln( "too high");
if(x<5) document.writeln("too low");

//y.innerHTML="Error: " + err + ".";
//}
}
</script>

<h1>My First JavaScript</h1>
<p>Please input a number between 5 and 10:</p>
<input id="demo" type="text">
<button type="button" onclick="myFunction()">Test Input</button>
<p id="mess"></p>

</body>

</html>



No comments:

Post a Comment