In this post we will discuss about JavaScript functions, how to create as well as how we can call a function. Also you can check my previous posts on:
- Constraints in SQL Server 2008
- Generics tutorial in C#.Net
- Creating a New ASP.NET 4 Web Site
A function is a block of statements that can performs some task as well it can also return some value. A function is independent of your program and not executed until called.
Functions must be declared before they can be used. Normally functions are placed in the <head> tag of the HTML document to ensure that they are defined before used.
In JavaScript, we can define a function by using the function keyword. Also a function can take some parameters.
Syntax:
function function_name ()
{
statement 1;
statement 2;
}
function function_name (parameter1, parameter 2)
{
statement 1;
statement 2;
}
Example:
Here in this example, we write a method inside the <head> </head> tag, which will give an alert message. And in a hyperlink we are calling that method in the body like below:
<head runat="server">
<title>JavaScript Function Example</title>
<script language="JavaScript" type="text/javascript">
function HelloWord() {
alert('Hello World !!!')
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<a href="javascript:HelloWord()">Click here </a>
</div>
</form>
</body>
see fig below:
simiarly if you want to call the javascript methos in a asp button control, then you can call the method in the OnClientClick event like below:
<asp:Button ID="btnCall" runat="server" Text="Call JavaScript Function" OnClientClick="HelloWord();" />
- Constraints in SQL Server 2008
- Generics tutorial in C#.Net
- Creating a New ASP.NET 4 Web Site
A function is a block of statements that can performs some task as well it can also return some value. A function is independent of your program and not executed until called.
Functions must be declared before they can be used. Normally functions are placed in the <head> tag of the HTML document to ensure that they are defined before used.
In JavaScript, we can define a function by using the function keyword. Also a function can take some parameters.
Syntax:
function function_name ()
{
statement 1;
statement 2;
}
function function_name (parameter1, parameter 2)
{
statement 1;
statement 2;
}
Example:
Here in this example, we write a method inside the <head> </head> tag, which will give an alert message. And in a hyperlink we are calling that method in the body like below:
<head runat="server">
<title>JavaScript Function Example</title>
<script language="JavaScript" type="text/javascript">
function HelloWord() {
alert('Hello World !!!')
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<a href="javascript:HelloWord()">Click here </a>
</div>
</form>
</body>
see fig below:
simiarly if you want to call the javascript methos in a asp button control, then you can call the method in the OnClientClick event like below:
<asp:Button ID="btnCall" runat="server" Text="Call JavaScript Function" OnClientClick="HelloWord();" />