Solved

How include other .js file function inside another .js file

Posted on 2009-04-03
8
1,413 Views
Last Modified: 2012-05-06
I have two javascript files
file1.js and file2.js
inside file1.js at following location I need to call a function which is actually included in file2.js
how can I do it.

Actually I have both of these js files added on the asp.net page but then user click on a button on that page which runs news_click function and inside that fuction I need to call the function from the file2.js file

Thanks
file1.js
function news_click(id) 
{
...
...
$(".ui-dialog-titlebar-close").hide();
$("#news_lett_dia").append(news_success).fadeIn("slow");
 
////Here I need to call the function from file2.js
}

Open in new window

0
Comment
Question by:shieldguy
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 15

Expert Comment

by:fsze88
ID: 24057836
<html>
<head><title></title></head>

<body>

<script type="text/javascript">
function createJavascript(){
  var fileref=document.createElement('script');
  fileref.setAttribute("type","text/javascript");
  fileref.setAttribute("src", 'file2.js');
  document.getElementsByTagName("head")[0].appendChild(fileref);
}

</script>

<a href="#" onclick="createJavascript();">createJavascript</a>
<a href="#" onclick="alertabc();">alertabc()</a>

</body>
</html>
file2.js
 
function alertabc(){
alert('abc');
}

Open in new window

0
 
LVL 1

Author Comment

by:shieldguy
ID: 24057891
No thats not what I asked for, I dont need to add the code on the web page instead I need to call the function with in the another js file
0
 
LVL 16

Accepted Solution

by:
sunithnair earned 400 total points
ID: 24057894
You will just need to include the file2.js file in your aspx page. This will allow you to call a function inside file2.js from inside a function inside file1.js
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script type="text/javascript" src="file2.js"></script>
    <script type="text/javascript" src="file1.js"></script>
</head>
<body>
<input type="button" value="Click Me" onclick="news_click(this);" />
</body>
</html>

Open in new window

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 16

Expert Comment

by:sunithnair
ID: 24057915
Sorry forgot to mention the order of script inclusion is important when you want to call a function in one js to be called from another.
0
 
LVL 15

Assisted Solution

by:fsze88
fsze88 earned 50 total points
ID: 24057937
Just like this?
<a href="#" onclick="news_click(); ">news_click()</a>

file1.js
====================
function news_click(){
fun2();
}

file2.js
====================
function fun2(){
alert('abc');
}
0
 
LVL 5

Assisted Solution

by:Pantalaim0n
Pantalaim0n earned 50 total points
ID: 24058137
Not even the order of inclusion is important, sunithnair.

fsze88:
Just define your script files in whatever order in your head section, as sunithnair showed, and you're good to go.
0
 
LVL 5

Expert Comment

by:Pantalaim0n
ID: 24058143
I meant shieldguy instead of fsze88..
0
 
LVL 1

Author Closing Comment

by:shieldguy
ID: 31566155
thanks
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn how to dynamically set the form action using jQuery.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question