How to call a JavaScript Function in Html


If we want to call the JavaScript function in the Html then we have to follow the steps which are given below. Using these steps, we can easily access the JavaScript function.

Step 1: Firstly, we have to type the script tag between the starting and closing of <head> tag just after the title tag. And then, type the JavaScript function.

<Head>      
<Title>     
Call a JavaScript function  
</Title>  
<script type = "text/javascript">  
         function msgprint() {  
            alert("You are Successfully Called the JavaScript function");  
         }  
</script>  
</Head>  

Step 2: After then, we have to call the javaScript function in the Html code for displaying the information or data on the web page. So, we can call the function of JavaScript by its name in the input tag of the form tag.

<form>  
         <input type = "button" onclick = "msgprint()" value = "Message Print">   
      </form>     

Step 3: And, at last, we have to save the Html file and then run the file which calls the JavaScript function.

<!Doctype Html>  
<Html>     
<Head>      
<Title>     
Call a JavaScript function  
</Title>  
<script type = "text/javascript">  
         function msgprint() {  
            alert("You are Successfully Called the JavaScript function");  
         }  
</script>  
</Head>  
<Body>   
<p>Click on the following button for calling the function: </p>        
      <form>  
         <input type = "button" onclick = "msgprint()" value = "Message Print">   
      </form>       
      <p>  
</Body>   
</Html>  

The output of above Html code is shown in the following screenshot:

How to call a JavaScript Function in Html

Leave a Reply

Your email address will not be published. Required fields are marked *