views:

28

answers:

1

I've read that including the scripts dynamically may provide some better performance, however i'm not really seeing that on the small local tests I'm doing. I created a jquery plugin to dynamically load other plugins as necessary and am curious as to if this is actually a good idea. The following would be called onready or at the bottom of the page(I can provide the source for the plugin if anyone is interested):

  $.fn.executePlugin(
    'qtip',  // looks in default folder
    {
    required: '/javascript/plugin/easing.js',  // not really required for qtip just testing it
    version: 1,  //used for versioning and caching
    checkelement: '#thumbnail',  // will not include plugin if $(element).length==0
    css: 'page.css',  // include this css file as well with plugin
    cache:true,   // $.ajax will use cache:true
    success:function()  {  // success function to be called after the plugin loads - apply qtip to an element
        $('#thumbnail').qtip(
        {
        content: 'Some basic content for the tooltip', // Give it some content, in this case a simple string
        style: {name:'cream'},
        });                   

    }
});   
+1  A: 

I think loading Javascript on demand is a good idea because it reduces bandwidth and page load times. A Microsoft Research Project (Doloto) takes this concept even further: They generate empty function stubs and lazily fetch code when it is invoked.

However, on many sites you already know at compile time which Javascript the page requires and can leverage the numerous optimization methods (combination, compression, minification, etc.) in a build step to produce the one ideal Javascript request at the bottom of the page.

I've found that a combination of the above often works well, loading essential Javascript in one compiled request at the bottom of the page and javascript required by features on demand.

Josef