Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2009-04-03
8
Medium Priority
?
1,423 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

715 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