Skip to content

Instantly share code, notes, and snippets.

@dustintheweb
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dustintheweb/eb8046b61d334730cd91 to your computer and use it in GitHub Desktop.
Save dustintheweb/eb8046b61d334730cd91 to your computer and use it in GitHub Desktop.
Optimize the Typekit load w/ Async & add a callback
<!-- tk optimize // http://goo.gl/2A1IF7 -->
<script type = "text/javascript" > (function(e) {
var t = 3e3;
if (window.sessionStorage) {
if (sessionStorage.getItem("useTypekit") === "false") {
t = 0;
}
}
var n = {
kitId: "abcd1234",
scriptTimeout: t
},
r = e.documentElement,
i = setTimeout(function() {
r.className = r.className.replace(/\bwf-loading\b/g, "") + " wf-inactive";
if (window.sessionStorage) {
sessionStorage.setItem("useTypekit", "false")
}
}, n.scriptTimeout),
s = e.createElement("script"),
o = false,
u = e.getElementsByTagName("script")[0],
a;
r.className += "wf-loading";
s.src = "//use.typekit.net/" + n.kitId + ".js";
s.async = true;
s.onload = s.onreadystatechange = function() {
a = this.readyState;
if (o || a && a != "complete" && a != "loaded") return;
o = true;
clearTimeout(i);
try {
Typekit.load(n)
} catch (e) {}
};
u.parentNode.insertBefore(s, u)
})(document) </script>
/*
In CSS
.wf-loading {
body > .loader {
visibility: visible;
opacity:1;
}
}
.wf-active, .wf-inactive {
//
body > .loader {
visibility: hidden;
opacity:0;
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment