I'm trying to dynamically load some jQuery libraries into the document head.
<head>
<script src="../js/jQuery.js"></script>
<script src="../js/App.js"></script>
</head>
jQuery.js looks like this:
(function() {
/*
* load all jQuery components
*
* @private
*/
var head = document.getElementsByTagName('head')[0];
var components = [];
var jQuery = document.createElement('script');
jQuery.type = 'text/javascript';
jQuery.src = 'http://' + location.host + '/js/jquery-1.8.2.min.js';
components.push(jQuery);
var jQueryMobile = document.createElement('script');
jQueryMobile.type = 'text/javascript';
jQueryMobile.src = 'http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js';
components.push(jQueryMobile);
var jQueryMobileCSS = document.createElement('link');
jQueryMobileCSS.type = 'text/css';
jQueryMobileCSS.href = 'http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css';
components.push(jQueryMobileCSS);
var meta = document.createElement('meta');
meta.name = 'viewport';
meta.content = 'width=device-width, initial-scale=1';
components.push(meta);
components.forEach( function( component ) {
head.appendChild( component );
});
// EDIT - FIX:
var App = document.createElement('script');
jQueryMobile.type = 'text/javascript';
jQueryMobile.src = 'http://' + location.host + '/js/App.js';
jQuery.onload = function() {
head.appendChild(App);
};
})();
The console.log shows that the head object contains the script tags loading the jQuery libraries. App.js requires jQuery but is throwing an error stating that jQuery is not defined. What am I missing?