Skip to content

Instantly share code, notes, and snippets.

@laphilosophia
Created May 3, 2017 07:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laphilosophia/559337aa3fdd683473bc64c2d44ac962 to your computer and use it in GitHub Desktop.
Save laphilosophia/559337aa3fdd683473bc64c2d44ac962 to your computer and use it in GitHub Desktop.
(function() {
'use strict';
var app = (function() {
var modules = [];
var init = function() {
fonts;
};
var ready = function(fn) {
if(document.readyState !== 'loading') {
fn();
} else {
document.addEventListener('DOMContentLoaded', fn);
}
};
var fonts = function(){
'use strict';
document.documentElement.className = 'fallback';
var css_href = '../fonts/fonts.css';
var localStorageSupported = function() {
try {
localStorage.setItem('fonts', 'fonts');
localStorage.removeItem('fonts');
return true;
} catch(e) {
return false;
}
}
if (localStorageSupported() && localStorage.spdemowebFonts) {
injectRawStyle(localStorage.getItem('spdemowebFonts'));
} else {
window.onload = function() {
injectFontsStylesheet();
}
}
function injectFontsStylesheet() {
var xhr = new XMLHttpRequest();
xhr.open('GET', css_href, true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
injectRawStyle(xhr.responseText);
localStorage.setItem('spdemowebFonts', xhr.responseText);
}
}
xhr.send();
}
function injectRawStyle(text) {
var style = document.createElement('style');
style.innerHTML = text;
document.getElementsByTagName('head')[0].appendChild(style);
document.documentElement.className = 'webFont';
}
};
return {
init: init,
ready: ready
};
})();
window.fonts = app;
})();
fonts.ready(function() {
fonts.init();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment