Last active
August 29, 2015 14:21
-
-
Save MisterPoppet/3fb66db0e83f76f7b8bd to your computer and use it in GitHub Desktop.
Async Twig Macros
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
{% macro asyncCSS(urls) %} | |
{% for url in urls %}asyncCSS({{ url }});{% endfor %} | |
function asyncCSS(url) { | |
var wf = document.createElement('link'); | |
wf.href = ('https:' == document.location.protocol ? 'https:' : 'http:') + url; | |
wf.rel = 'stylesheet'; | |
wf.media = 'only x'; | |
var s = document.getElementsByTagName('style')[0]; | |
s.parentNode.insertBefore(wf, s); | |
} | |
{% endmacro %} | |
{% macro asyncJS(urls) %} | |
{% for url in urls %}asyncJS({{ url }});{% endfor %} | |
function asyncJS(url) { | |
var wf = document.createElement('script'); | |
wf.src = ('https:' == document.location.protocol ? 'https:' : 'http:') + url; | |
wf.type = 'text/javascript'; | |
wf.async = 'true'; | |
var s = document.getElementsByTagName('script')[0]; | |
s.parentNode.insertBefore(wf, s); | |
} | |
{% endmacro %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment