LoadJS is a small Javascript Library based on jQuery Deferred for loading Javascript files on demand, only when needed and with caching system. There are many javascript libraries online but mostot them are limited on loading a single file . LoadJS has an unlimited number of parameters and loading is not parallel.
- load javascript only when needed
- Javascript is loaded only once, on further requests it will not reload any file
- Make your web app render faster even on slow macchines
- Load multiple javascript files with order and dependencies
First step just load jQuery.js (1.7>) and load-js.js:
<scriptsrc="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script><scriptsrc="/js/load-js.js"></script>Then just use it:
// create a global LoadJS object to use around your web appvarloadjs=newLoadJS();//now use loadjs object to load javascript, //example I need to load tinymce.js only when user opens an editor//the first time user open the editor this call will take some millisecondsloadjs.load('/tinymce/tinymce.min.js').done(function(){openEditor();});//if now the user reopen the editor, the done function will return immediatelyloadjs.load('/tinymce/tinymce.min.js').done(function(){openEditor();});// create a global LoadJS object to use around your web appvarloadjs=newLoadJS();//We have the following situation//Library a.js //Library b.js depends on a.js//Library c.js depends on b.js//We need to load this javascript only when needed and in this order: a.js -> b.js -> c.jsloadjs.load('a.js','b.js','c.js').done(function(){startSomeFunction();});//if later I need only a.js in another context, this function will be called immediatelyloadjs.load('a.js').done(function(){anotherFunction();});