Javascript Includes
When you start messing with Prototype, Scriptaculous and other Javascript libraries, you often end up with a dozen lines of Javascript script tags pulling in all of the code from different places. That would be fine if I never found a new library to add or never swapped them out. That results in the fun task of digging through all of the individual templates/places where it ended up. Even with disciplined templating, you can still end up with multiple places for the same thing.
So, last night, I added a Javascript include to my setup (though not yet on this site). I can now create a single Javascript loader for each project that contains all of the scripts necessary. That gives me a single entry to put into my pages and templates. Then, when (not if) I need to add or change which libraries are included in my pages, I don't need to touch a single template. I just change the multiloader.
<script src="http://www.wynia.org/js/multiloader.js" type="text/javascript"></script>
This is what's in the multiloader.
$js_path = "http://www.wynia.org/js/";
function js_include($script){
var script = document.createElement('script');
script.src = $js_path + $script;
script.type = 'text/javascript';
var head = document.getElementsByTagName('head').item(0);
head.appendChild(script);
}
//Main AJAX Library
js_include("prototype.js");
Any Javascript can now be pulled in similar to how other languages do includes using the js_include() function.

May 26th, 2006 at 11:07 am
Good small solution.
July 25th, 2006 at 9:48 am
How do you cope with the problem of functions being called before they have loaded?
July 11th, 2007 at 12:18 pm
The problem with they way your using it that the included script won't get parsed untill the current script is finished being parsed. here is a better solution for realtime inclusion.
http://www.exit12.org/archives/12
August 31st, 2007 at 6:51 am
Nice solution, but like seant23 said, there will be a timing problem that will need to be addressed, but the solution linked to by seant23 introduces the new problem of limiting included scripts to the same domain, unlike your solution.
See Ajile @ http://ajile.ikitz.com/ for a fully cross-browser, all around solution that handles both the timing and same domain problems.
August 31st, 2007 at 11:11 pm
Ooops, meant http://ajile.iskitz.com/.
October 12th, 2007 at 3:10 am
I wrote a script like this that includes javascript files via AJAX calls:
http://www.forgeniuses.com/?p=14
June 30th, 2008 at 12:10 pm
@whatchawant: there is no cross domain policy issues because I'm not using the standard script tag, I'm evaluating text…
November 14th, 2008 at 11:36 am
Generally from a programming perspective, I would want to declare variables that are only used inside the function in the function itself. Scope is a wonderful thing, not that I have not forgotten to use it myself from time to time.
$js_path = "http://www.wynia.org/js/";