joranquinten
2/4/2016 - 9:27 AM

hookAssets

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);
  }
};