GZipped: 631 bytes
Minified: 1079 bytes
Compatibility: Tested in Safari, Chrome, Firefox, IE6+, basically doing nothing too fancy.
Examples: http://berklee.github.com/nbl/example.html
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!
- Load scripts asynchronously or in order, or any combination of both
- Every script can have its own callback
- A callback for loading all items
- Can be mixed with your provided function for loading some data
- [TODO: Trigger a callback on a user definable timeout period]
- Uses HTML5’s data attribute for configuration, so load all scripts with one script-tag
- Very small code!
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="[ { load: 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js',
then: ['jquery.lightbox.min.js', 'jquery.carousel.min.js' ],
callback:function(e){ jquery_loaded(e) }
},
'http://www.google-analytics.com/urchin.js' ]"></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.
NBL.js is rather flexible in its options, so let’s dissect a few examples.
[ 'script1.js', 'script2.js', 'script3.js' ]
This will simply load all three scripts in parallel.
[ {load:'script1.js', then:['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.
{ load:'script1.js', then:{load:'script2.js', then:{load:'script3.js, then:'script4.js } } }
It’s a bit crazy, but nesting the objects like this will allow you to load all scripts sequentially.
[ {load:'script1.js', callback:function(e){ script1_callback(e) }},
{load:'script2.js', callback:function(){ script2_callback() }},
{load:'script3.js', callback:function(){ script3_callback() }} ]
[ { load:'script1.js',
then: {load:'plugin1.js', callback:function(){ plugin1_callback() } },
callback: function(e){ script1_callback(e) }
},
{load:'script2.js', callback:function(){ script2_callback() }} ]
{ load: ['script1.js', 'script2.js', 'script3.js' ], timeout: function(){global_timeout()} }{
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.
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)
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.
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.