How to Call JavaScript Function in Another JavaScript File

Overview

I was working on a project and there is a scenario that I have to call a function which is defined in a JavaScript File in another JavaScript File. So, in the code below you can see that how we can call the function in another JavaScript file.

Code:

Step 1: We have the following function in Scripts/Hello JavaScript File:

  1. function HelloWorld() {  
  2.     //print Hello World  
  3.     $("#UserMessage").html("Hello World");  
  4. }  

Step 2: Now we have to call it in another JavaScript File as named Activity. Follow the code given below to call it:

  1. $.getScript('/Scripts/Hello.js'function () {          
  2.       HelloWorld();  
  3. });