Skip to content

Instantly share code, notes, and snippets.

@joranquinten
Created February 4, 2016 09:27
Show Gist options
  • Save joranquinten/c7e9cb9397a7d0bb42aa to your computer and use it in GitHub Desktop.
Save joranquinten/c7e9cb9397a7d0bb42aa to your computer and use it in GitHub Desktop.
hookAssets
/**
Dynamically hook (an array of) assets
**/
var hookAssets = function(urls) {
var pattern = /(?:\.([^.]+))?$/;
if (typeof(urls) === 'string') urls = url.split(','); // force it to by an array
for (var i = 0; i < url.length; i++) {
var url = urls[i];
type = pattern.exec(url)[1]; // get type based on extension
var s = null;
if (type === 'js') {
// generate javascript tag
s = document.createElement('script');
s.type = 'text/javascript';
s.src = url || null;
}
if (type === 'css') {
// generate stylesheet tag
s = document.createElement('style');
s.type = 'text/css';
s.href = url || null;
}
header = document.head || document.getElementsByTagName('head')[0];
if (s) header.appendChild(s);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment