Skip to content

berklee/nbl

Repository files navigation

NBL.js 2.0 – a tiny non-blocking JavaScript lazy loader

GZipped: 593 bytes
Minified: 977 bytes
Single line: 816 bytes
Compatibility: Tested in Safari, Chrome, Firefox, IE6+, basically doing nothing too fancy.
Examples: http://berklee.github.com/nbl/example.html

What is NBL.js?

NBL.js is a tiny script that will make your HTML pages load faster by loading all your JavaScript files
asynchronously (in parallel) with the rest of your page. Normally if you include two or three scripts
in your page, the browser will wait for them to be executed before your page is shown.

By using NBL.js the browser can start showing the HTML while loading and executing the scripts, resulting
in a faster, more responsive website. All in less than 1kb!

Features

  • Load scripts asynchronously or in order, or any combination of both
  • Every script can have its own callback
  • Trigger a callback on a user definable timeout period
  • Uses HTML5’s data attribute for configuration, so load all scripts with one script-tag
  • Also available as a single line for inclusion in your HTML page
  • Less than 1kb!

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:

  • It will load the latest version of jQuery.
  • It will load the Urchin script from Google Analytics.
  • After jQuery has loaded, it will start loading the jQuery plugins as defined after jQuery in parallel.
  • When jQuery has loaded, it will call the jquery_loaded() function.
  • 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 example 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 900 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( [ 'options', 'here' ] ).

You can find more examples in the included example.html.

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

Berklee

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

NBL Plus: support for images and CSS updated

GitHub user Knowlecules mailed me with modifications to preload CSS and images using NBL.js. I’ve incorporated
that code into nbl.plus.js (and nbl.plus.min.js). Thanks to some additional bug squashing by Richard Lopes,
the latest version of NBL Plus is now better than ever! Clocking in at 1154 bytes for the minified version and
694 bytes for the gzipped one, there’s no reason not to use NBL Plus for your asynchronous media loading needs.

MIT License

Copyright © 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.

About

NBL is a tiny non-blocking JavaScript lazy loader

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published