Skip to content

Instantly share code, notes, and snippets.

@MisterPoppet
Last active August 29, 2015 14:21
Show Gist options
  • Save MisterPoppet/3fb66db0e83f76f7b8bd to your computer and use it in GitHub Desktop.
Save MisterPoppet/3fb66db0e83f76f7b8bd to your computer and use it in GitHub Desktop.
Async Twig Macros
{% 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