Web Design

                                 JAVASCRIPT functions

Call the function

<html>
<title>call the function</title>
<head>
<script language="javascripts" type="text/javascript">
function myfunction()
{
alert("function called....")
}
</script>
</head>
<body>
<form>
<input type="button" onclick="myfunction()"value="call function">
</form>
</body>
</html>

Loop break

<html>
<title>loop break</title>
<body>
<script language="javascripts" type="text/javascript">
for(var i=1;i<=10;i++)
{
    if(i==8)
    {
        break;
    }
    document.write("loop is runs "+i);
    document.write("<br/>")
}
 document.write("loop is break ")
</script>
</body>
</html>

Loop table

<html>
<title>loops</title>
<body>
<table border="5">
<script language="javascripts" type="text/javascript">
for(i=1;i<=6;i++)
{
document.write("<tr><td>col 1 row "+i);
document.write("</td>")
document.write("<td> ");
document.write("<td>col 2 row "+i);
document.write("</td>")
document.write("</tr>");
}
</script>
</table>
</body>
</html>

Example function 1 (find age)

<html>
<head>
<script type="text/javascript">
function show_prompt()
{
var age=prompt("please enter your age");
if(age==null)
{
alert("please enter your age!!");
}
else if(isNaN(age))
{
alert("invalid age please try again");
}
else if(age!=null)
{
    if(age<18)
    {
    alert("you are a child");
    }
    else
    {
    alert("yor are a adult");
    }
}
}
</script>
</head>
<body>
<input type="button" onclick="show_prompt()" value="show a prompt box"/>
</body>
</html>

Alert box

<html>
<head>
<script language="javascripts" type="text/javascript">
function show_alert()
{
alert("I am an alert box");
}
</script>
</head>
<body>
<form>
<input type="button" onclick="show_alert()" value="show alert box">
</form>
</body>
</html>

Loop continue

<html>
<title>loop continue </title>
<body>
<script language="javascripts" type="text/javascript">
for(var i=0;i<=10;i++)
{
    if(i==5)
    {
        continue;
    }
    document.write("loop number is "+i)
    document.write("<br/>")
}
</script>
</body>
</html>

Onforcus

<html>
<head>
<script language="javascripts" type="text/javascript">
function setStyle(x)
{
document.getElementById(x).style.background="yellow";
}
</script>
</head>
<body>
First name:<input type="text" onfocus="setStyle(this.id)"id="fname"/>
<br/>
last name:<input type="text" onfocus="setStyle(this.id)"id="lname"/>
</body>
</html>

Example function 2

<html>
<head>
<script language="javascripts" type="text/javascript">
function product(a,b)
{
return a*b;
}
</script>
</head>
<body>
<script language="javascripts" type="text/javascript">
document.write(product(3,4));
</script>
</body>
</html>

Example function 3 calculate

<html>
<head>
<script type="text/javascript">
function cal()
{
var num_1=parseInt(prompt("please enter number ","no 1"));
var c    =prompt("please enter condition that you want to calculate");
var num_2=parseInt(prompt("please enter number","no 2"));
 if(isNaN(num_1) || isNaN(num_2) || c=="")
    {
    alert("please type valid input");
    }
else if(num_1!=null && num_2!=null)
    {
        if(c=="+")
        {
        var add= num_1 + num_2;
        alert("adition is " + add);
        }

        else if(c=="-")
        {
        var sub=num_1 - num_2;
        alert("subtraction is " +sub);
        }
        else if(c=="*")
        {
        var multi=num_1 * num_2;
        alert("multiplication is "+multi);
        }
    }
}

</script>
</head>
<body>
<input type="button" onclick="cal()" value="cal"/>
</body>
</html>

This free website was made using Yola.

No HTML skills required. Build your website in minutes.

Go to www.yola.com and sign up today!

Make a free website with Yola