Skip to content

Instantly share code, notes, and snippets.

@thomasmb
Created August 11, 2014 14:03
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thomasmb/b67680a04ae75bc301b7 to your computer and use it in GitHub Desktop.
Save thomasmb/b67680a04ae75bc301b7 to your computer and use it in GitHub Desktop.
Google Webfonts
WebFontConfig = {
google: { families: [ 'PT+Sans:400,400italic:latin', 'Ubuntu:300,400,500:latin' ] }
};
var cb = function() {
var wf = document.createElement('script');
wf.src = '//ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
wf.type = 'text/javascript';
wf.async = 'true';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(wf, s);
};
var raf = requestAnimationFrame || mozRequestAnimationFrame || webkitRequestAnimationFrame || msRequestAnimationFrame;
if(raf){
raf(cb);
}else{
window.addEventListener('load', cb);
}
@kosmiq
Copy link

kosmiq commented Mar 16, 2015

Hey, I know this is kind of old now (well, less than a year). But I found it via Google and got around using it for a project. It works great in all modern browsers but all hell breaks loose in IE9 and IE8. Yeah, sure not the most important browsers to support but I was aiming for IE9 support atleast.

The error occurs in requestAnimationFrame which is not supported by IE9 and lower, and it stops the page from rendering (IE11 in IE9 mode crashes). Here's the code with a SHIM added (that I found via Google). It's from Paul Irish and I simply added it to your scropt. The entire script is now:

      WebFontConfig = {
        google: { families: [ \'Ek+Mukta:200,800:latin\' ] }
      };
        var cb = function() {
            var wf = document.createElement(\'script\');
            wf.src = \'//ajax.googleapis.com/ajax/libs/webfont/1/webfont.js\';
            wf.type = \'text/javascript\';
            wf.async = \'true\';
            var s = document.getElementsByTagName(\'script\')[0];
            s.parentNode.insertBefore(wf, s);
        };

        // shim layer with setTimeout fallback
        window.requestAnimFrame = (function(){
          return  window.requestAnimationFrame       ||
                  window.webkitRequestAnimationFrame ||
                  window.mozRequestAnimationFrame    ||
                  function( callback ){
                    window.setTimeout(callback, 1000 / 60);
                  };
        })();

        //var raf = requestAnimationFrame || mozRequestAnimationFrame || webkitRequestAnimationFrame || msRequestAnimationFrame;
        var raf = requestAnimFrame;

        if(raf){
            raf(cb);
        }else{
            window.addEventListener(\'load\', cb);
        }

@kosmiq
Copy link

kosmiq commented Mar 16, 2015

I've also created an updated GIST here: https://gist.github.com/kosmiq/2cdac1677d0ba07fc846

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