I am adding jQuery in dynamically due to the requirements of my project. However, when the below function is called jQuery is not defined is displayed in the console. However, if I write alert(jQuery) in the console it returns function (a,b){return new m.fn.init(a,b)}, so I know that it is loaded. Does anyone have any suggestions?
function AddExternals(){
if (!jQuery){
var jq = document.createElement("script");
jq.type = "text/javascript";
jq.src = "https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js";
document.getElementsByTagName("head")[0].appendChild(jq);
console.log("Added jQuery!");
} else {
console.log("jQuery already exists.")
}
}
AddExternals()is called? Remember, you're checking forjQueryafter the DOM has completely loaded.document.addEventListener("DOMContentLoaded", AddExternals)window.onload = function() { AddExternals() };jQuery is not defined? you must be calling something else which relies on it as yourconsole.log()in the code you shared doesn't say that anywhere - I'm guessing you're calling something after this?if (!jQuery){.