Skip to content

Instantly share code, notes, and snippets.

@gnh1201
Last active December 29, 2019 05:56
Show Gist options
  • Select an option

  • Save gnh1201/c5ed5ff6ad9132e2e1e648f8cd2f7fa7 to your computer and use it in GitHub Desktop.

Select an option

Save gnh1201/c5ed5ff6ad9132e2e1e648f8cd2f7fa7 to your computer and use it in GitHub Desktop.
Dynamically (Programmatically) JS/CSS file load with callback function
// Dynamically (Programmatically) JS/CSS file load with callback function
// Go Namhyeon <[email protected]>
function loadJS(url, callback) {
var el = document.createElement("script");
el.src = url;
el.type = "text/javascript";
document.head.appendChild(el);
if(typeof(callback) == "function") {
el.onload = callback(el);
}
return el;
}
function loadCSS(url, callback) {
var el = document.createElement("link");
el.href = url;
el.rel = "stylesheet";
el.type = "text/css";
document.head.appendChild(el);
if(typeof(callback) == "function") {
el.onload = callback(el);
}
return el;
}
// load jQuery and jQuery UI (JS)
loadJS("https://code.jquery.com/jquery-3.4.1.min.js", function(el) {
alert("JS loaded: " + el.src);
alert(typeof($));
loadJS("https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js", function(el) {
alert("JS loaded: " + el.src);
});
});
// load jQuery UI (CSS)
loadCSS("https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css", function(el) {
alert("CSS loaded: " + el.href);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment