Last active
July 3, 2017 21:46
-
-
Save sryze/2ba993aa98b00dfea99fd540bcc9248a to your computer and use it in GitHub Desktop.
Handlebars helpers for adding references to scripts / stylesheets
This file contains 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
// Usage: {{scriptRef "your_script.js"}} | |
Handlebars.registerHelper('scriptRef', function(url) { | |
var scripts = document.head.getElementsByTagName('script'); | |
for (var i = 0; i < scripts.length; i++) { | |
if (scripts[i].src && scripts[i].src.indexOf(url) !== -1) { | |
return; | |
} | |
} | |
var script = document.createElement('script'); | |
script.src = url; | |
document.head.appendChild(script); | |
}); | |
Handlebars.registerHelper('stylesheetRef', function(url) { | |
var links = document.head.getElementsByTagName('link'); | |
for (var i = 0; i < links.length; i++) { | |
if (links[i].href && links[i].href.indexOf(url) !== -1) { | |
return; | |
} | |
} | |
var link = document.createElement('link'); | |
link.rel = 'stylesheet'; | |
link.href = url; | |
document.head.appendChild(link); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment