NBL.js 2.0

(a tiny non-blocking JavaScript lazy loader)

Fork me on GitHub

Home   —   Examples   —   Documentation

Usage

Include NBL.js in your pages and let it dynamically load all your JavaScript files by simply including the following tag:

<script src="nbl.js" data-nbl="[ [ 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', 'jquery.lightbox.min.js', 'jquery.carousel.min.js' ], function(e){ jquery_loaded(e) }, 'http://www.google-analytics.com/urchin.js', function(){ urchin_loaded() } ]"></script>

This will do the following:

  1. It will load the latest version of jQuery.
  2. It will load the Urchin script from Google Analytics.
  3. After jQuery has loaded, it will start loading the jQuery plugins as defined after jQuery in parallel.
  4. When jQuery has loaded, it will call the jquery_loaded() function.
  5. Finally, when Urchin has loaded, it will call the urchin_loaded() function.

Verifying the results

After NBL.js has done its job you can verify a few things through the global nbl object. Every script will be placed in the nbl.q object, referred to by the filename of the script minus the ".js" or .min.js" extension or any non-word characters.

In the above example "jquery.lightbox.min.js" will become "jquerylightbox". If it has loaded successfully, nbl.q.jquerylightbox will return true, otherwise you'll get the script element of the script you queried.

When a script fails to load, NBL will fire the first defined function it encounters after a default timeout of 2500ms. In the above example that function is jquery_loaded(). If jQuery loads fine, but one of the plugins doesn't, the timeout will expire and call jquery_loaded() once again, only this time it will provide the nbl.q object as its only argument e.

That way you can distinguish between a normal call and a timed out call, check out the examples for more information on this feature.

Options

NBL.js is rather flexible in its options, so let's dissect a few examples.

Loading three scripts asynchronously:

[ 'script1.js', 'script2.js', 'script3.js' ]

This will simply load all three scripts in parallel.

Loading two scripts asynchronously, and two plugins asynchronously after the first script:

[ [ 'script1.js', 'plugin1.js', 'plugin2.js' ], 'script2.js' ]

This will load script1.js and script2.js in parallel. After script1.js has loaded, plugin1.js and plugin2.js will load in parallel.

When NBL.js encounters an array of scripts, it will immediately load the first script (script1.js in this case) and load the remaining scripts (plugin1.js and plugin2.js) after completion. The plugin1.js and plugin2.js scripts have a lower priority than the script1.js and script2.js scripts and will be loaded after script1.js completes.

Loading four scripts in order:

[ [ 'script1.js', [ 'script2.js', [ 'script3.js, 'script4.js ] ] ] ]

It's a bit crazy, but nesting the arrays like this will allow you to load all scripts sequentially. After the first array with script1.js, NBL.js encounters a second array starting with script2.js, which it will load after script1.js has completed.

After script2.js completes, NBL.js will continue the iteration with the third array that starts with script3.js, finally ending with loading script4.js after script3.js has completed.

Three scripts with their own callbacks:

[ 'script1.js', function(e){ script1_callback(e) }, 'script2.js', function(){ script2_callback() }, 'script3.js', function(){ script3_callback() } ]

The basic rule of callbacks is: declare the callback function directly after the script.

In this example, the three scripts will load in parallel and upon completion of each script, the corresponding callback will be called. In case of a timeout, the first defined function (script1_callback(e)) will be called with nbl.q as argument e (as explained above).

Two scripts and a plugin with their own callbacks:

[ [ 'script1.js', 'plugin1.js', function(){ plugin1_callback() } ], function(e){ script1_callback(e) }, 'script2.js', function(){ script2_callback() } ]

Following the basic rule of callbacks as mentioned above, we place the callback function for script1.js outside the array that contains script1.js and plugin1.js, since to NBL.js script1.js and script2.js are on equal footing, the callbacks for both must be placed in the main array.

Defining a global timeout function and a new timeout:

[ 3200, function(e){ global_timeout(e) }, 'script1.js', function(){ script1_callback() }, 'script2.js', function(){ script2_callback() } ]

First off, by specifying a number anywhere in the options, NBL.js will assume you want to change the timeout from the default 2500ms to the provided number. Second, by putting a function before any scripts, it will define it as the global timeout function.

In this case script1_callback() and script2_callback() will be called when script1.js and script2.js are finished loading. And in case of an error, global_timeout() will be called after approximately 3500ms.

Alternative usage

If you prefer you can choose to simply include NBL.js in a single line in your HTML pages. This way you can save a HTTP-call from the browser, and it will only add 882 bytes to your HTML page. Simply include the code in nbl.single.js into a <script> tag at the end of your page, and replace ['your', 'scripts', 'here'] with your own options.

You can't use the data-nbl attribute of the script tag if you use this method.

Final note

All options are case-sensitive, if you include a file called urCHin.js, the corresponding nbl.q object will be nbl.q.urCHin. I advise you to simply use lowercase for all options.

If you do not specify any options in the script tag, NBL.js will instantiate the default nbl object and will do nothing. You will have to do a manual nbl.l( [ 'your', 'options', 'here' ] ).

You can find more examples in the included examples.

I hope you find NBL.js useful, thanks for reading this!

Berklee

(@Berklee on Twitter or feedback at berkl.ee)

MIT License

	Copyright (c) 2009-2011 Berklee

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
				


Home   —   Examples   —   Documentation


Homepage: http://berklee.github.com/nbl/   —   GitHub: http://github.com/berklee/nbl/