Bootstrap.js requires jquery to run. If you want to benefit from loading an asynchronous script, then you probably want to also download asynchronous jquery (and possibly other libraries) ... The problem is that you have no guarantee that asynchronous jquery will complete before loading using the above above code example. I am also sure that you have your own javascript that you want to write in order to use bootstrap.js functions. This means even more dependencies. You can write the logic for connecting dependencies and asynchronous loading manually, but it will be a lot of work, as the number of scripts that you may need to include will increase.
Requirejs is a library that takes care of all this dependency management and can load your files asynchronously (and in the correct order). Another advantage of this library is the optimizer, which can track dependencies and "write" them to one (optionally reduced) file. After you use the optimizer to optimize your βmainβ js file (a file with all the dependencies needed for the page), requireJS can simply load this file asynchronously. You only need to include one script!
An example would look like this:
/app/main.js:
requirejs.config({ paths: { jquery: "lib/jquery-1.11.0", bootstrap: "lib/bootstrap" }, shim: { bootstrap: { deps: ['jquery'] } } }); //Define dependencies and pass a callback when dependencies have been loaded require(["jquery", "bootstrap"], function ($) { //Bootstrap and jquery are ready to use here //Access jquery and bootstrap plugins with $ variable });
In this case
jquery.js and bootstrap.js will be in the / app / lib directory (along with require.js).
In your HTML code you will have a script like this:
<script src="/app/lib/require.js" data-main="/app/main"></script>
This will load both jquery (in the correct order) at boot and then pass these modules as parameter (s) (only jquery / $ is needed, since bootstrap is just a plugin on top of jquery) into your callback function.
source share