Last active
December 29, 2019 05:56
-
-
Save gnh1201/c5ed5ff6ad9132e2e1e648f8cd2f7fa7 to your computer and use it in GitHub Desktop.
Dynamically (Programmatically) JS/CSS file load with callback function
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
| // 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