Created
January 10, 2020 17:07
-
-
Save daxxog/4a20152759e29e98a2b757db2e5bc5ab to your computer and use it in GitHub Desktop.
Load jQuery library using plain JavaScript (With callback)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//- https://www.sitepoint.com/dynamically-load-jquery-library-javascript/ | |
//- Load jQuery library using plain JavaScript (With callback) | |
//- --------------------------------------------------------------------- | |
(function () { | |
function loadScript(url, callback) { | |
var script = document.createElement("script") | |
script.type = "text/javascript"; | |
if (script.readyState) { //IE | |
script.onreadystatechange = function () { | |
if (script.readyState == "loaded" || script.readyState == "complete") { | |
script.onreadystatechange = null; | |
callback(); | |
} | |
}; | |
} else { //Others | |
script.onload = function () { | |
callback(); | |
}; | |
} | |
script.src = url; | |
document.getElementsByTagName("head")[0].appendChild(script); | |
} | |
loadScript("https://code.jquery.com/jquery-3.4.1.min.js", function () { | |
//jQuery loaded | |
console.log('jquery loaded'); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment