Create a gist now

Instantly share code, notes, and snippets.

@skddc /typekit.js
Last active Jul 21, 2018

Embed
What would you like to do?
Improved Typekit embed code
(function(d) {
var tkTimeout=3000;
if(window.sessionStorage){if(sessionStorage.getItem('useTypekit')==='false'){tkTimeout=0;}}
var config = {
kitId: 'a1b2c3f4',
scriptTimeout: tkTimeout
},
h=d.documentElement,t=setTimeout(function(){h.className=h.className.replace(/\bwf-loading\b/g,"")+" wf-inactive";if(window.sessionStorage){sessionStorage.setItem("useTypekit","false")}},config.scriptTimeout),tk=d.createElement("script"),f=false,s=d.getElementsByTagName("script")[0],a;h.className+=" wf-loading";tk.src='//use.typekit.net/'+config.kitId+'.js';tk.async=true;tk.onload=tk.onreadystatechange=function(){a=this.readyState;if(f||a&&a!="complete"&&a!="loaded")return;f=true;clearTimeout(t);try{Typekit.load(config)}catch(e){}};s.parentNode.insertBefore(tk,s)
})(document);
@lekkerduidelijk

This comment has been minimized.

Show comment
Hide comment
@lekkerduidelijk

lekkerduidelijk Apr 26, 2014

Wow, this really helped me out. Thanks for sharing!

Wow, this really helped me out. Thanks for sharing!

@brianfeister

This comment has been minimized.

Show comment
Hide comment
@brianfeister

brianfeister Apr 28, 2014

Small note, just as an FYI for anyone who doesn't know. The default embed code has it's own prescriptive JS for how FOUT should be handled.

That code looks like this (at the time of this writing):

    <script type="text/javascript" src="//use.typekit.net/yourKitID.js"></script> <!-- e.g. "a3b0btq.js" -->
    <script type="text/javascript">try{Typekit.load();}catch(e){}</script>

Your code is helpful for anyone who wants to take control of how font loading happens on their website, but since Typekit has so much happening behind the scenes, I prefer to use their "default" code because if they change something about how fonts are loaded, it should automatically be updated and not become out of sync, whereas your code is more likely to be affected by some kind of internal organization-level deprecation.

Small note, just as an FYI for anyone who doesn't know. The default embed code has it's own prescriptive JS for how FOUT should be handled.

That code looks like this (at the time of this writing):

    <script type="text/javascript" src="//use.typekit.net/yourKitID.js"></script> <!-- e.g. "a3b0btq.js" -->
    <script type="text/javascript">try{Typekit.load();}catch(e){}</script>

Your code is helpful for anyone who wants to take control of how font loading happens on their website, but since Typekit has so much happening behind the scenes, I prefer to use their "default" code because if they change something about how fonts are loaded, it should automatically be updated and not become out of sync, whereas your code is more likely to be affected by some kind of internal organization-level deprecation.

@renatocarvalho

This comment has been minimized.

Show comment
Hide comment
@renatocarvalho

renatocarvalho Mar 7, 2016

Is this script still working?

Is this script still working?

@skddc

This comment has been minimized.

Show comment
Hide comment
@skddc

skddc Apr 10, 2016

@renatocarvalho We're still using it on our sites with no problems since we created it.

Owner

skddc commented Apr 10, 2016

@renatocarvalho We're still using it on our sites with no problems since we created it.

@ChrisSki

This comment has been minimized.

Show comment
Hide comment
@ChrisSki

ChrisSki Jan 24, 2017

works great for me! awesome work @skddc

works great for me! awesome work @skddc

@JayOwl

This comment has been minimized.

Show comment
Hide comment
@JayOwl

JayOwl Feb 6, 2017

Still working, thanks for the fix!

JayOwl commented Feb 6, 2017

Still working, thanks for the fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment