Solved

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

Posted on 2009-04-03
8
1,411 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
JDeveloper 12c for 32 bit 4 35
Menu Inconsistent 3 21
Change properties of a dialog box 1 22
ASP.Net Session State alternatives 3 32
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now