Created
February 4, 2016 09:27
-
-
Save joranquinten/c7e9cb9397a7d0bb42aa to your computer and use it in GitHub Desktop.
hookAssets
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 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