A simple function for loading JS & CSS files asynchronously
- Licensed MIT
Place the getJS function inline in the head of your page (it can also be included in an external JavaScript file if preferable).
Then call it by passing it a JavaScript URL:
<head>
...
<!-- include getJS here... -->
<script src="getJS.js"></script>
<!-- just example... -->
<script>
getJS( url, callback );
</script>
...
</head>Load a single file with getJS:
getJS( "/path/to/script.js" );You can execute code after the File has loaded via a callback:
getJS( "/path/to/script.js", function() {
// The file has loaded
});You can load multi files via Array:
// Example Without Callback!
getJS( ["/path/to/script.js","/path/to/style.css",...] );
// Example With Callback!
getJS( [
"https://code.jquery.com/jquery-latest.min.js",
"/path/to/script.js",
"/path/to/style.css",
"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css",
...
], function() {
// All files has loaded
});getJS Will automatically added hostname location.origin if not find it in path.
getJS( "/javascript/script.js" );This file will append to <head> like <script src="http://localhost/javascript/script.js" async></script>