jQuery is not available immediately as you are loading it asynchronously (by appending it to the <head>
). You would have to add an onload listener to the script (jqTag
) to detect when it loads and then run your code.
e.g.
function myJQueryCode() {
//Do stuff with jQuery
}
if(typeof jQuery=='undefined') {
var headTag = document.getElementsByTagName("head")[0];
var jqTag = document.createElement('script');
jqTag.type="text/javascript";
jqTag.src="https://stackoverflow.com/questions/6813114/jquery.js";
jqTag.onload = myJQueryCode;
headTag.appendChild(jqTag);
} else {
myJQueryCode();
}