Skip to content

Instantly share code, notes, and snippets.

@timmarinin
Created March 20, 2014 07:41
Show Gist options
  • Save timmarinin/9659044 to your computer and use it in GitHub Desktop.
Save timmarinin/9659044 to your computer and use it in GitHub Desktop.
<style src="..."> loader

This code allows you to use style tag with src attribute, just like script. Merely for fun. Don't use it on production, because browser could not cache and load styles asynchronously.

(function() {
  var styleTags = document.querySelectorAll('style');
  for (var i = 0, l = styleTags.length; i < l; ++i) {
    (function(el){
      var src = el.getAttribute('src');
      if(src.length > 0) {
        var oReq = new XMLHttpRequest();
        oReq.onload = (function(){
          return function() {
            el.innerText = this.responseText;
          }
        })();
        oReq.open("get", src, true);
        oReq.send();
      }
    })(styleTags[i]);
  }
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment