Skip to content

Instantly share code, notes, and snippets.

@gosseti
Created December 6, 2012 16:36
Show Gist options
  • Save gosseti/4225858 to your computer and use it in GitHub Desktop.
Save gosseti/4225858 to your computer and use it in GitHub Desktop.
A Typekit snippet that uses Modernizr.load (yepnope) to asynchronously load webfonts only for in browsers that support @font-face.
Modernizr.load([{
test: Modernizr.fontface,
complete: function() {
TypekitConfig = {
kitId: 'xxxxxxx',
scriptTimeout: 3000
};
(function() {
var t = setTimeout(function() {
}, TypekitConfig.scriptTimeout);
var tk = document.createElement('script');
tk.src = '//use.typekit.com/' + TypekitConfig.kitId + '.js';
tk.onload = tk.onreadystatechange = function() {
var rs = this.readyState;
if (rs && rs != 'complete' && rs != 'loaded') return;
clearTimeout(t);
try { Typekit.load(TypekitConfig); } catch (e) {}
};
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(tk, s);
})();
}
}]);
@gosseti
Copy link
Author

gosseti commented Jun 5, 2014

Modernizr.load [
  test: Modernizr.fontface
  complete: ->
    TypekitConfig =
      kitId: "xxxxxxx"
      scriptTimeout: 3000
    (->
      t = setTimeout(->
      , TypekitConfig.scriptTimeout)
      tk = document.createElement("script")
      tk.src = "//use.typekit.com/" + TypekitConfig.kitId + ".js"
      tk.onload = tk.onreadystatechange = ->
        rs = @readyState
        return  if rs and rs isnt "complete" and rs isnt "loaded"
        clearTimeout t
        try
          Typekit.load TypekitConfig
        return

      s = document.getElementsByTagName("script")[0]
      s.parentNode.insertBefore tk, s
      return
    )()
    return
]

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