Click here to Skip to main content
15,920,383 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone!Can we Call the function written in one JS File in another JS file?Cna anyone help me how to Call the function ?
Posted
Updated 18-Jun-19 3:42am

Yeah, you surely can. You just need to include both JS files in your ASPX pages as follows:

XML
<script type="text/javascript" src="js1.js"></script>
<script type="text/javascript" src="js2.js"></script>


I added two scripts in my sample app which are as follows:

js1.js : Contains the following method

C#
function js1() {
    alert("Hello from js1");
    js2();
}


And, js2.js : Contains the following method

SQL
function js2() {
    alert("Hello from js2"

);
}

Please note the js1() function of js1.js invokes the js2() function, which is written in js2.js.

And, I called the js1() method of js1.js in my aspx page as follows:

XML
<script language="javascript">
       js1();
</script>


It gave me alert messages from both JS files:

"Hello from js1"
"Hello from js2"
 
Share this answer
 
Comments
hemantwithu 28-Sep-10 1:37am    
thanks
Al-Farooque Shubho 28-Sep-10 1:38am    
Wouldn't it be nice to mark it as answer if you think it deserves?:)
Create 2 Js File
file1.js
file2.js
and add reference in this sequence.

HTML
<script type="text/javascript" src="file1.js"></script>
<script type="text/javascript" src="file2.js"></script>
<script type="text/javascript">
    alertOne( );
</script>


JavaScript
// File1.js
function alertNumber( n ) {
    alert( n );
};
// File2.js
function alertOne( ) {
    alertNumber( "one" );
};
 
Share this answer
 
Comments
Member 13518332 21-May-18 2:39am    
not working
C#
Code:

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


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

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


$.getScript('/Scripts/Hello.js', function () {          
      HelloWorld();  
});  
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900