Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Call Javascript class from another js file

Status
Not open for further replies.

GoTerps88

Programmer
Apr 30, 2007
174
US
I have one javascript js file that writes out a calendar. Right now I'm just using the regular string concatenation.

I wanted to use a more efficient process, so I found a nice js StringBuilder class.

How would I call or include this separate class in another js file in an existing js file? Both of these files reside in the same directory.
 
When you put any function or variable declarations in a .js file, they are all loaded into the current page's function and variable definitions when the page loads. So, it is perfectly legal to have a function in one .js file that references a function in another .js file.

Here's an example:

Code:
function foo() {
   alert("test");
}
Code:
function bar() {
   foo();
}
Code:
<script type="text/javascript" src="test.js"></script>
<script type="text/javascript" src="test2.js"></script>
<script type="text/javascript">

bar();

</script>

So, to answer this question:
How would I call or include this separate class in another js file in an existing js file?

The answer would be: the same way that you would reference the class if it was in the same js file. [thumbsup2]

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top