Skip to content

Instantly share code, notes, and snippets.

@charltoons
Last active December 14, 2015 18:39
Show Gist options
  • Save charltoons/5131083 to your computer and use it in GitHub Desktop.
Save charltoons/5131083 to your computer and use it in GitHub Desktop.
Import.js -- The last javascript file you will ever include in HTML. This small file programmatically includes all of your javascript files, either in the body or the head, according to a config object. See the instructions for more info.
(function(){
var files =
[
/****** Instructions
1) insert the following into your HTML file:
<script type="text/javascript" src="import.js"></script>
2) then populate the following object with the info of your javascript files:
{
src: 'test1.js' //the string you would put in the "src" attribute of the script element
},
{
src: 'test2.js',
inBody: true //optional, inserts script tag in the body instead of the head
}
***********************/
];
function js_import(src, inBody){
inBody = typeof inBody !== 'undefined' ? inBody : false;
var newFile = document.createElement('SCRIPT');
newFile.type = 'text/javascript';
newFile.src = src;
document.getElementsByTagName(((inBody) ? 'body' : 'head'))[0].appendChild(newFile);
}
for (var i=0; i<files.length; i++){
var file = files[i];
if (file.inBody) document.addEventListener('DOMContentLoaded', function(){ js_import(file.src, true); });
else js_import(file.src, false);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment