« On Handling Spam
Traversing a JavaScript Array Using Callbacks »


JSManager.js - Easily manage JS dependancies

I spent some time today refractoring some existing helper functions I had written in the past into a full library.

The result is JSManager.js, a extremely compact (2.3kb) library for managing JavaScript dependencies.

What it does:

  • JSManager lets you load other JS files dynamically.
  • JSManager provides the means to manage file inclusion across multiple scripts, and moves your script includes out of the HTML.

For example, if you have a script file that needs a specific jQuery plugin loaded into the page, but you don't always know if that file will be loaded in the HTML you can do something like this:

JSManager.Register('jQuery.js');

JSManager.Register('jQuery.plugin.js');

JSManager.Init(

    function ()

    {

        // Once all registered scripts are loaded this runs

    }

);

JSManager keeps track of all registered scripts, calling Init() repeatedly will only register scripts added since the last call to Init().

This allows multiple script files to each register their own scripts without repeatedly loading the same file, or causing conflicts.

Registered scripts are sequentially loaded in the order they are registered to avoid conflicts.

Of course, if you want to load a script immediately, you can simply use Load():

JSManager.Load('SomeFile.js',

    function ()

    {

        // once the script fully loads

        // this will run.

    });

Callbacks are always optional, so if you have no need for a callback you don't need to pass it to either Init() or Load().

Summary:

  • JSManager adds one object to the global namespace (JSManager) and will not cause any conflicts with other scripts.
  • JSManager exposes 3 public functions:
    • Register (ScriptName): Registers a script file for loading when Init is called.
    • Init ([Optional CallBack]): Loads all registered scripts and runs the callback when finished.
    • Load (ScriptName, [Optional Callback]): Loads (and registers) a individual script immediately. Will run the callback when finished if provided.

JSManager is MIT licensed, so you can do just about anything with it (except for sue me). If you found it useful, drop me a comment.

Posted by Jonathan Holland on 2/16/2009.

Tags: Javascript   JSManager   Open-Source

Comments:

Great job, this is a very compact and handy script, and beats loading a "everything and the kitchen sink" script for this purpose.

I'll be using it often.

Gravatar Posted by Stewart on 2/17/2009.

See https://wiki.mozilla.org/ServerJS/Modules

Gravatar Posted by Patrick Mueller on 2/17/2009.

Patrick, I don't have high hopes for a standard cross browser approach to JS loading implemented in all popular browsers within 5 years.

Gravatar Posted by Jonathan Holland on 2/17/2009.

This definitely has advantages over the tactic I wrote about. I like how all the dependencies can be self contained within the script that depends on them.

Gravatar Posted by John MacIntyre on 2/17/2009.

Comments are closed on this post.