tags:

views:

271

answers:

6

what is the recommended way of including a Javscript file from another Javascript file?

A: 

How about this:

(original link)

<script type="text/javascript">
// Function to allow one JavaScript file to be included by another.
// Copyright (C) 2006-08 www.cryer.co.uk
function IncludeJavaScript(jsFile)
{
  document.write('<script type="text/javascript" src="'
    + jsFile + '"></scr' + 'ipt>'); 
}
</script>

and then to include a second JavaScript file simply add the line:

IncludeJavaScript('secondJS.js');

The page that came from includes some gotchas that arise from this approach, so it's worth looking at before using the method.

Ben
If you use that after the document has loaded your are in for a surprise since document.write will replace your active document.
some
Also, there is no need to split the the closing tag in two parts. Since everything in the script tag according to (x)HTML DTD is PCDATA (parsed character data) you have to escape the end-tag </ like this: "<\/script>"
some
should be DOM assembled which makes the second point moot though
annakata
+3  A: 

There are libraries that'll do this for you. You can also add a script tag to your document pointing to the file you want to load (from js), which is simplest, but has problems.

http://developer.yahoo.com/yui/yuiloader/

http://www.appelsiini.net/projects/lazyload

Edit: I see a lot of answers that add a script tag to the head of your document. As I said this simple solution has a problem, namely you won't know when the browser has finished loading the script you requested, so you wont know when you can call this code. If you want to use a solution like this you should also add a callback somehow to tell you when the required code was loaded.

Vasil
adding a callback is a perfectly reasonable thing to do - at the end of the day every library's bootstrapping is just an implementation of this technique, Ryan Doherty just posted a simple example.
annakata
Lazy Load plugin is about lazy loading images. It does not load JavaScript files.
Mika Tuupola
+4  A: 

Most people add the JavaScript file to the head of the document:

<script type="text/javascript">
  var newfile=document.createElement('script');
  newfile.setAttribute("type","text/javascript");
  newfile.setAttribute("src", '/myscript.js');
  document.getElementsByTagName("head")[0].appendChild(newfile);
</script>
Ryan Doherty
@Ryan: Argh! I was just posting that! ;)
some
suggest edit to rewrite as util function
annakata
A: 

Theres also an function built into Scriptaculous that is very easy to use.

Scriptaculous.require("path/to/script.js");

Worth knowing, since Scriptaculous is a very common javascript library these days.

seanhodges
+1  A: 

jQuery has getScript() function. Also note that Lazy Load mentioned above is only for images. Not for JavaScript files.

$.getScript(url, [callback]);
Mika Tuupola
A: 

Dojo does it using dojo.require():

dojo.require("your.module.name");

Normally this is a synchronous operation done with XHR. But if you use the xDomain build it will be asynchronous and dojo.addOnLoad() will be raised when the script is loaded.

Read more about it:

Eugene Lazutkin