Hello, my name is Jan Jarfalk and I am an interaction designer and interface developer.

I’ve been working professionally with the web since 2002. Back then I had my own company and did everything myself. Now I am a bit more specific - I do usability, accessibility and a lot of client side coding. This, Unwrongest, is my personal lab. This is where I try, learn and evolve.

I am a Swedish citizen from Stockholm that currently lives and works in Sydney, Australia. From here I work for Getupdated's Stockholm based division 'Social Media', where we help our clients to create social networks.

I put function, before design. I love beautiful interfaces, but I like them simple and obvious. I like things that are fast and responsive. Take a look at my projects and I am certain you will notice and appreciate my slipstreamed approach.

This Jquery plugin is an on-demand plugin loader, also known as a lazy loader. Instead of downloading all Jquery-plugins, you might or might not need, when the page loads. Lazy downloads the plugins when you actually use them.

This Jquery plugin is an on-demand Jquery plugin loader, also known as a lazy loader. Instead of downloading all Jquery-plugins, you might or might not need, when the page loads. Lazy downloads the plugins when you actually use them. Lazy is very lightweight, super fast, and smart. Lazy will keep track of all your plugins and dependencies and make sure that they are only downloaded once.

How to use Lazy

$.lazy({
	src: 'jquery.elastic.js',
	name: 'elastic'
});

// And then you use you plugins as you always do
$('textarea').elastic();

How to work with dependencies

$.lazy({
	src: 'ui.draggable.js',
	name: 'draggable',
	dependencies: {
		js: ['ui.core.js'],
		css: ['ui.draggable.css']
	}
});

// And then you use you plugins as you always do
$("#draggable").draggable();

This will load ui.draggable.css and ui.core.js before it loads ui.draggable.js and applies it on the element draggable.

Enable or disable caching

$.lazy({
	src: 'ui.draggable.js',
	name: 'draggable',
	dependencies: {
		js: ['ui.core.js'],
		css: ['ui.draggable.css']
	},
	cache: true
});

Caching is disabled by default, but you can enable it by setting the variable cache to true.

Change log
Version Changes
1.3.1 Critical bug fix for argument leakage.
1.3 Faster, better and stronger. Added cache control and a new API
1.2.2 Fixed a bug where Lazy would load the same plugin more than once.
1.2 Added support for jquery’s chainability
1.1 Added support for css and javascript dependencies

Comments

  • [...] Tage schon bin ich über ein schönes kleines jQuery-Plugin names Lazy gestolpert. Bei lazy handelt es sich um ein nur 530byte großes Plugin das gerade bei großen [...]

  • 13 May 2009 | EllisGL says:

    Hmm, if we tie this to a php js combiner, compressor, that would make this even more beneficial.

  • This is an amazing solution for enterprise applications that uses a lot of plug-ins in it’s page, to replace it all once in the header, and make the lazy javascript handle the rest for it!

    Really great one.

  • 14 May 2009 | EllisGL says:

    Here’s a CSS compressor I did a while backago:
    $val)
    {
    $css .= str_replace(’; ‘,’;',str_replace(’ }’,'}’,str_replace(’{ ‘,’{',str_replace(array(”\r\n”,”\r”,”\n”,”\t”,’ ‘,’ ‘,’ ‘),”",preg_replace(’!/\*[^*]*\*+([^/][^*]*\*+)*/!’,”,file_get_contents($key.’.css’))))));
    }

    // Send a content type header and the content.
    header(’Content-type: text/css’);
    echo $css;

    // Flush to the browser.
    ob_end_flush();

    You would use it in the file like this:
    $date))
    {
    $date = date(’YmdHis’, filemtime($val.’.css’));
    }
    }
    return implode(’&’,$styles).’.’.$date.’.php.css’;
    }
    ?>

    <link rel=”stylesheet” type=”text/css” href=”" />

    Just would have to modify it to us js instead.

  • 15 May 2009 | EllisGL says:

    http://www.moxieforge.net/overview.php?project_id=18&category_id=6 <– could modify that for the JS part.

  • [...] up is Lazy, a on-demand plugin loader for jQuery. I have to be honest, two months ago I would have thought [...]

  • I agree with everyone. A potent addition by EllisGL and Kudos @Amr for putting it so simply and succinctly.

  • The proxy method would better return ‘this’ to keep jQuery’s chanability.
    eg.
    $(’#xxx’).elastic().bgiframe()….

  • [...] In: JQuery plugins 7 Jun 2009 Go to Source [...]

  • I like this plugin, great idea. I think it would be beneficial to allow js caching as an option though using…

    $.ajaxSetup({ cache: true });

  • [...] jQuery - Lazy - The on-demand jQuery plugin loader - Jan Jarfalk (Suggested by Elijah Manor) [...]

  • [...] Lazy - The on-demand jQuery plugin loader "This Jquery plugin is an on-demand plugin loader, also known as a lazy loader. Instead of downloading all Jquery-plugins, you might or might not need, when the page loads. Lazy downloads the plugins when you actually use them." (tags: web_development jquery javascript plugins) [...]

  • [...] jQuery - Lazy - The on-demand jQuery plugin loader - Jan Jarfalk (Suggested by Elijah Manor) [...]

  • 31 Aug 2009 | Pascal says:

    Do you think it might be possible to add a “callback” property so that we can launch a JS function when the .js is loaded?

    • That is definitely possible. I was in need of one my self just a few days ago. I might take a look at it in a few days.

  • 06 Oct 2009 | Robert says:

    Hi, I just tried out and it worked great but when I moved the code to an external JS, it doesn’t seem to any more. Any one else have this issue?

    • 15 Oct 2009 | Luis Cornejo says:

      make sure that the external js is loaded after jQuery

  • Looks like it’s not working properly with jGrowl..

    $(document).ready(function(){
    		$.lazy({
    			src: '/js/jgrowl/jquery.jgrowl_compressed.js',
    			name: 'jGrowl',
    			dependencies: {
    				css: ['/js/jgrowl/jquery.jgrowl.css']
    			}
    		});
    		$.jGrowl("foo", {"header":"Message:"});
    		$.jGrowl("bar", {"header":"Message:"});
    		$.jGrowl("bar", {"header":"Message:"});
    		$.jGrowl("bar", {"header":"Message:"});
    	});
    
    • Works if you call the method once, so it runs, then wait a sec to make sure it’s loaded:

      var dostuff = function() {
      $.jGrowl(”foo”, {”header”:”Message:”});
      $.jGrowl(”bar”, {”header”:”Message:”});
      $.jGrowl(”bar”, {”header”:”Message:”});
      $.jGrowl(”bar”, {”header”:”Message:”});
      }
      $(document).ready(function(){

      $.lazy({
      src: ‘/js/jgrowl/jquery.jgrowl_compressed.js’,
      name: ‘jGrowl’,
      dependencies: {
      css: ['/js/jgrowl/jquery.jgrowl.css']
      }
      });
      $.jGrowl(”foobar”, {”header”:”Message:”});
      setTimeout(”dostuff()”,1000);
      dostuff();
      });

  • Why didn’t you used classic callbacks like this :

    $.lazy(src: ‘dependencies/ui.draggable.js’, function(){

    // use draggable

    });

    ?

    Is there a reason ?

    I think it make your code heavier for nothing, and difficult to understand

    Also beginners could be tricked by the demos you provide… They could think that their programs will “wait” when they do $.lazy …

  • I’m having trouble getting this plugin to work with the jQuery UI files, and I can’t see where I’m going wrong? My code is this:

    $.lazy({
    src: ‘assets/javascript/jquery-ui-1.7.2/ui/minified/effects.core.min.js’,
    name: ‘effect’,
    dependencies: {
    js: ['/assets/javascript/jquery-ui-1.7.2/ui/minified/effects.pulsate.min.js']
    },
    cache: true
    });

    $.lazy({
    src: ‘/assets/javascript/jquery.localscroll-1.2.7-min.js’,
    name: ‘localScroll’,
    dependencies: {
    js: ['/assets/javascript/jquery.scrollTo-1.4.2-min.js']
    },
    cache: true
    });

    /* Function to highlite a target after the anchor has been clicked */
    function targetHighlight(target) {
    var target_id = $(target).attr(”id”); // get the id of the target element
    if(target_id != ‘content_main’) { // don’t apply the highlight to this list of targets
    $(target).effect(”pulsate”, { times: 1 }, 500);
    }
    }

    // TEST: if we have an internal page anchor use the scroller plugins to get smooth animation.
    if($(”a[href*=#]“).length > 0) {
    $.localScroll({
    axis:’xy’, // ‘x’, ‘y’, ‘xy’ or ‘yx’
    onAfter: targetHighlight,
    offset: {top:-20, left:-20}
    });
    }

    The localScroll works fine, and triggers properly - but the pulsate effect I expect to fire doesn’t happen. I’m guessing that the call to effect() is what’s going wrong, but I can’t figure out how to properly apply it?

Make a comment