10

Is there a solution out there where I can have JavaScript/jQuery autoload dependent files when needed? For example, consider this scenario:

  1. I have an autoloader script listening for when a particular script needs to be loaded.
  2. A jQuery dialog() plugin is called.
  3. The autoloader is told to listen for when this plugin is called, and loads the jQuery UI.
  4. If more dialogs are called in the future, the required script will not be loaded.

Is this too much effort for simply trying to limit bandwidth? Should I just include all of the core files in one superpackage and be done with it?

Thank you for your time.

Oliver Spryn
  • 16,871
  • 33
  • 101
  • 195

9 Answers9

3

There are many examples of on demand script loading out there. For example remy sharp has a code sample on his blog that you could either use as is or turn into a jQuery plugin. Unfortunately it may not work in all browsers.

There is also the jQuery Lazy Plugin Loader which loads jQuery plugins on demand rather than up-front. To use it you would need to set up lazy loading for each piece of jQuery UI you are using as follows (name will be the function name for each piece you use):

$.lazy([{
   src: 'jquery-ui-1.8.14.custom.min.js',
   name: 'dialog'
}]);

You can also use the techniques in this question about loading jQuery itself on demand. For example you can dynamically create a script tag at the time needed to load jQuery UI.

Finally since you are talking about jQuery UI consider getting it from Google's CDN, which is likely cached in the user's browser anyway.

Community
  • 1
  • 1
justkt
  • 14,610
  • 8
  • 42
  • 62
3

Yes you should inclde all of the scripts in one file. Or at least most of them groupped like this: jquery.js, global.js (that's where frequently - on more than one, two pages - used scripts should be) and page_specyfic.js. Imagine that a dialog() is called and the user has to wait for .js to download and plugins to initialise. Savings in bandwith (if any) wouldn't be worth harming the users expirience.

Litek
  • 4,888
  • 1
  • 24
  • 28
  • 2
    Hmm... I for the sake of code organization, I'd like to keep all of these file seperate. For example, a jQuery file, a jQuery UI, script for this plugin, script for that plugin... It is easier and more organized in my opinion. Please look a my code post below. You can see the zillion scripts (with more to come) that are loaded per page. In order to keep these organized, with minimal HTTP requests and bandwidth usage, how can I combine these? – Oliver Spryn Jun 21 '11 at 21:10
  • 1
    You have to decide whitch files to combine to global one and witch ones are used only on one page. There are numerous tools to minify files. For example [this one](http://www.refresh-sf.com/yui/). For the sake of organization I'd recommend leaving coments describing given plugin, and it's version (also leaving comments is sometimes required by licence). Btw you should do the same with your css files. – Litek Jun 21 '11 at 21:25
2

You can try this new jquery plugin. Works like yeapnope.js but more make sense.

http://plugins.jquery.com/plugin-tags/autoloader

$(document).autoLoader(
    {
      test: $.ui,
      loadScript: "https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-   ui.min.js",
      complete: function(){
         console.log($.ui);
      }
    }
);
adam
  • 21
  • 1
1

I wouldn't worry too much. The files are cached. Once one page in your site loads the jquery UI (or any other include file like CSS), the next time it's needed it will be in the user's browser cache, never to be loaded again for days/weeks

Rodolfo
  • 4,155
  • 23
  • 38
1

Sounds like you want a script loader.

You can't generally do synchronous loading of scripts across browsers, though, so script loaders are necessarily asynchronous. What you're asking for isn't exactly possible since the script needs to load, call a callback, and then continue. You wouldn't want to call some plugin and not know whether it is executing synchronously or not, that gets you into a world of problems.

I recommend you look at DeferJS, a script loader for jQuery: https://github.com/BorisMoore/jsdefer

InfinitiesLoop
  • 14,349
  • 3
  • 31
  • 34
1

From your comments, part of your wish seems to be to keep your code organized. I would recommend RequireJs. It lets you break your code up into clearly separated modules with explicit dependencies. Then when you want to go to production, there's a build tool that will merge them all back together into the (request/bandwidth saving) 2-3 files you want to serve.

Sean McMillan
  • 10,058
  • 6
  • 55
  • 65
0

Yeah, I have also thought about implementing something like this. I am not sure if it would be worthwhile or not in the end but there are quite a few libraries to do this for you like ensure

John Kalberer
  • 5,690
  • 1
  • 23
  • 27
0

you could try something like this but it would be a pain. basically you are checking the type of error caught and message if dialog (the function you are trying to call doesn't exist) load the function and try calling the method again. Like I said it would be a pain to do this everywhere unless some elegant solution was thought of.

function loadDialog() {
    $('#myDialog').dialog({});
}

try { 
    loadDialog()
} catch(e) {
    if (e && e.type && e.type=='not_defined' && e.message == 'dialog is not defined') {
        //load jQuery plugins...
        loadDialog();
    }
}
Nathan Romano
  • 7,016
  • 3
  • 19
  • 18
  • Given that there are existing script loader solutions out there, I'm not sure the OP necessarily needs to reinvent his or her own unless it's for the practice of learning how to do it. – justkt Jun 21 '11 at 21:10
  • yea use a defer loader at all possible or just make sure your library is loaded before using it. – Nathan Romano Jun 21 '11 at 21:11
0

This is a follow-up post for a comment above:

<link rel="stylesheet" href="../system/stylesheets/universal.css" />
<link rel="stylesheet" href="../system/stylesheets/jquery-ui.min.css" />
<link rel="stylesheet" href="../system/stylesheets/uploadify.css" />
<link rel="stylesheet" href="system/stylesheets/style.css" />
<script src="../system/javascripts/swfobject.js"></script>

<script src="../system/javascripts/jquery.min.js"></script>
<script src="../system/javascripts/jquery-ui.min.js"></script>
<script src="../system/javascripts/global.jquery.js"></script>
<script src="../system/javascripts/analog.jquery.js"></script>
<script src="../system/javascripts/funtip.jquery.js"></script>
<script src="../system/javascripts/uploadify.jquery.js"></script>
<script src="system/javascripts/install.jquery.js"></script>
<link rel="stylesheet" href="system/templates/stylesheets/style.css" />
<script>
$(document).ready(function() {
    $(':text, :password, textarea').funtip();
});
</script>
Oliver Spryn
  • 16,871
  • 33
  • 101
  • 195