13

I wonder what is the best way to load fonts from Google Fonts. I googled and founded <link/> way better than @import. Are there any tools to measure speed and performance over network and browser? What about javascript method?

<link href='http://fonts.googleapis.com/css?family=Oswald:300' rel='stylesheet' type='text/css'>

or

@import url(http://fonts.googleapis.com/css?family=Oswald:300);

or

<script type="text/javascript">
  WebFontConfig = {
    google: { families: [ 'Oswald:300:latin' ] }
  };
  (function() {
    var wf = document.createElement('script');
    wf.src = ('https:' == document.location.protocol ? 'https' : 'http') +
      '://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);
  })(); </script> 
Nishantha
  • 6,065
  • 6
  • 33
  • 51
  • Possible duplicate of [Including Google Web Fonts link or import?](http://stackoverflow.com/questions/12316501/including-google-web-fonts-link-or-import) – Alex Angas Mar 02 '16 at 00:39

1 Answers1

8

<link> is preferred to @import, as it doesn't block parallel downloads. See this link for more details: http://www.stevesouders.com/blog/2009/04/09/dont-use-import/

If the fonts you want to use are static and served by Google, the JavaScript solution is probably worse off performance-wise than both <link> and @import, since it has to load an external script (//ajax.googleapis.com/ajax/libs/webfont/1/webfont.js) which then, after what I can see, injects the same link element you could just put directly in your HTML source.

Jacob Bundgaard
  • 943
  • 11
  • 29
  • While this may be the case for a home baked js solution, Google provides a loader script which allows the developer to control loading i.e. asynchronous or async and adds a number of relevant events. It also allows management of multiple source CDN's. See [link] ( https://github.com/typekit/webfontloader) – Sinthia V Jun 16 '17 at 17:50