Javascript Includes

May
04
2006

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.

 

Comments on this post

Feedback is always welcome. Read some from other folks or leave your own below. Just keep things civil and remember that what you post lives on in public. Forever.

Thanks,
J

8 Responses to “Javascript Includes”

  1. George Says:

    Good small solution.

  2. Bryce Says:

    How do you cope with the problem of functions being called before they have loaded?

  3. seant23 Says:

    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

  4. whatchawant Says:

    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.

  5. whatchawant Says:

    Ooops, meant http://ajile.iskitz.com/.

  6. Levi Senft Says:

    I wrote a script like this that includes javascript files via AJAX calls:

    http://www.forgeniuses.com/?p=14

  7. seant23 Says:

    @whatchawant: there is no cross domain policy issues because I'm not using the standard script tag, I'm evaluating text…

  8. Alex Boese Says:

    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/";

Leave Your Own Comment

By submitting a comment, you agree to license it under the terms of the Creative Commons Attribution license.

People who post comments get the added benefit of visiting the site without advertising.

© 2003-2009 J Wynia. All original content is licensed under the terms of the Creative Commons Attribution license unless otherwise noted. Content from other sources is licensed under its original terms.