Last active
December 19, 2015 07:48
-
-
Save matthewhudson/5920796 to your computer and use it in GitHub Desktop.
Loading WebFonts via TypeKit: http://blog.typekit.com/2011/05/25/loading-typekit-fonts-asynchronously/
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
<style>.wf-loading{visibility:hidden}</style> | |
<script type="text/javascript"> | |
(function (doc) { | |
var config = { | |
kitId: 'abc1def', | |
scriptTimeout: 3000 | |
}; | |
var getElement = function (tagName) { | |
return doc.getElementsByTagName(tagName)[0]; | |
} | |
var htmlElement = getElement('html'); | |
var timeoutCallback = setTimeout(function () { | |
htmlElement.className = htmlElement.className.replace(/(\s|^)wf-loading(\s|$)/g, ' '); | |
}, config.scriptTimeout); | |
var isLoaded = false; | |
var script = doc.createElement('script'); | |
script.src = '//use.typekit.net/' + config.kitId + '.js'; | |
script.async = true; | |
script.onload = script.onreadystatechange = function() { | |
var rs = this.readyState; | |
if (isLoaded || rs && rs != 'complete' && rs != 'loaded') return; | |
isLoaded = true; | |
clearTimeout(timeoutCallback); | |
try { | |
Typekit.load(config); | |
} catch (e) { | |
} | |
}; | |
doc.head.appendChild(script); | |
})(document); | |
</script> |
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 type="text/javascript"> | |
(function() { | |
var config = { | |
kitId: 'abc1def', | |
scriptTimeout: 3000 | |
}; | |
var h = document.getElementsByTagName('html')[0]; | |
h.className += ' wf-loading'; | |
var t = setTimeout(function() { | |
h.className = h.className.replace(/(\s|^)wf-loading(\s|$)/g, ' '); | |
h.className += ' wf-inactive'; | |
}, config.scriptTimeout); | |
var d = false; | |
var tk = document.createElement('script'); | |
tk.src = '//use.typekit.net/' + config.kitId + '.js'; | |
tk.type = 'text/javascript'; | |
tk.async = 'true'; | |
tk.onload = tk.onreadystatechange = function() { | |
var rs = this.readyState; | |
if (d || rs && rs != 'complete' && rs != 'loaded') return; | |
d = true; | |
clearTimeout(t); | |
try { Typekit.load(config); } catch (e) {} | |
}; | |
var s = document.getElementsByTagName('script')[0]; | |
s.parentNode.insertBefore(tk, s); | |
})(); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment