PDA

View Full Version : Javascript Includes?


Nick
2006.01.27, 12:32 AM
I'm just getting more into Javascript and I wondered how to include another Javascript file into my file. The C/C++ equivalent would be #include "otherFile.c" but I can't find a similar Javascript function. Is this not supported?

akb825
2006.01.27, 01:49 AM
*views source of this very page* *sees what Nick wants*
<script type="text/javascript" src="JavaScript file.js"></script>
Also, you could always use PHP includes. (which work like the C/C++ equivalents)

Nick
2006.01.27, 02:02 AM
I mean how can include a file into my .js file. I didn't think I could use HTML code like that in my .js file. I suppose I wasn't very clear in the question.

I also have realized that if I include the other .js file simply before the other .js file, the second file can use functions from the first so I suppose that might be why what I'm looking for doesn't exist.

Taxxodium
2006.01.27, 02:13 AM
javascript must be used in conjunction with HTML. So you need to include all the js files in the HTML file. I don't think the order in which you add them is important. The general rule of thumb is to include first and then use the functions.

akb825
2006.01.27, 02:18 AM
Ok, I see what you're getting at. Unfortunately, after checking both w3schools and google, that's the only thing I can see. I don't think JavaScript supports that kind of includes.

Delfi
2006.07.24, 03:58 AM
I'm just getting more into Javascript and I wondered how to include another Javascript file into my file. The C/C++ equivalent would be #include "otherFile.c" but I can't find a similar Javascript function. Is this not supported?

This might be helpful:

ttp://www.wynia.org/wordpress/2006/05/04/javascript-includes/

http://www.mashhoor.ws/2006/07/19/javascript-includes/

The Scriptaculous library has been doing something like this for a while now. But they recommend not using the DOM as it fails in Safari
[code]
var Scriptaculous = {
Version: ‘1.6?,
require: function(libraryName) {
// inserting via DOM fails in Safari 2.0, so brute force approach
document.write('’);
}
[code]


Best