JavaScript Functions example


In this post, we will discuss JavaScript functions, how to create as well as how we can call a function. Also, you can check my previous posts on:
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();” />
Check out Best Alternative to InfoPath -> Try Now

You May Also like the Following SharePoint Online Tutorials:

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →