Solved

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

Posted on 2009-04-03
8
1,418 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 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

623 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