Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2009-04-03
8
Medium Priority
?
1,428 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 1600 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 200 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 200 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

916 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