javascript - Would on-the-fly .js inclusion (using jQuery) be considered bad or best practice? -


I have created a basic javascript "framework" for my web app, one of the ways on this is:

  is included: function (url, success, cache) {$. AJAX (URL: url, data type: "script", success: success, cache: cache! == incorrect}}}   

this allows me to include the jQuery plugin , And act on them, on-the-fly. This way:

  Core.include ('coolPlugin.js', function () {$ obj.cool (); // ...}, true);   

In this way I only need to include the actual script tag for the initial import of jQuery from Google CDN and My Core Library. After that, all the actions Asinink Add the script to the script file because it is needed, and the first connection is cached on.

If I wrap core.include in $ (document) .ready (), this mandatory form It alters the import from a delay for the circumstances, where it will be necessary.

I have realized that the primary drawback of this time is that it is time to load in a .js file. Are there other drawbacks missing? It seems that this one Great practice, but does not want to ignore some emerging catastrophe.

As told above, J.S. Should be included, considered bad or best practice?

There are actually some valid arguments for this "deferred" loading technique. Primarily this enhances the initial full page load time, then "on demand" or after a while the script can be loaded.

The answer to "bad or best practice" of your question is "It depends." If your site is very heavy, and can be effectively spoken in later loadable parts, it is a technique that can work well for you. If your site requires less JS requirements, doing so may actually hurt your performance and will definitely add to the complexity of your code which may not be needed.

I will consider it among one of my many devices, and not necessarily "good" or "bad".

In addition, check these libraries, they provide some of these deferred functionality among other features, you may find them interesting:



Comments