Created
October 27, 2014 05:30
-
-
Save brainwire/965aa04a601b34432a7f to your computer and use it in GitHub Desktop.
font
This file contains 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
<script> | |
(function(){ | |
function addFont() { | |
var style = document.createElement('style'); | |
style.rel = 'stylesheet'; | |
document.head.appendChild(style); | |
style.textContent = localStorage.sourceSansPro; | |
} | |
try { | |
if (localStorage.sourceSansPro) { | |
// The font is in localStorage, we can load it directly | |
addFont(); | |
} else { | |
// We have to first load the font file asynchronously | |
var request = new XMLHttpRequest(); | |
request.open('GET', '/path/to/source-sans-pro.woff.css', true); | |
request.onload = function() { | |
if (request.status >= 200 && request.status < 400) { | |
// We save the file in localStorage | |
localStorage.sourceSansPro = request.responseText; | |
// ... and load the font | |
addFont(); | |
} | |
} | |
request.send(); | |
} | |
} catch(ex) { | |
// maybe load the font synchronously for woff-capable browsers | |
// to avoid blinking on every request when localStorage is not available | |
} | |
}()); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment