最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - JavaScript Autoloader? - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question asked Jun 21, 2011 at 20:57 Oliver SprynOliver Spryn 17.3k33 gold badges104 silver badges200 bronze badges
Add a comment  | 

9 Answers 9

Reset to default 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.

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.

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);
      }
    }
);

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

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

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.

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

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();
    }
}

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>
发布评论

评论列表(0)

  1. 暂无评论