Created
May 3, 2017 07:39
-
-
Save laphilosophia/559337aa3fdd683473bc64c2d44ac962 to your computer and use it in GitHub Desktop.
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
(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