zoxon
11/24/2016 - 8:47 AM

XHR the stylesheet (asynchronous) loading

XHR the stylesheet (asynchronous) loading

// https://meowni.ca/posts/web-fonts/

document.body.onload = function() {
  var xhr = new XMLHttpRequest();
  var url = 'https://fonts.googleapis.com/css?family=Arima+Madurai:300,400,500';

  xhr.open('GET', url, true);
  xhr.onreadystatechange = function () {
    if (xhr.readyState == 4 && xhr.status == 200) {
      var style = document.createElement('style');
      style.innerHTML = xhr.responseText;
      document.head.appendChild(style);
    }
  };
  
  xhr.send();

}