10

I am trying to optimize the loading speed of my mobile webpage, and for that effect I am using the website:

This website evaluates my source and tells me what I can improve. In my website I download a font:

<link href="//fonts.googleapis.com/css?family=Open+Sans:400,700" rel="stylesheet">

and aparently this is blocking the rendering of my page. Now, if this was JavaScript I could use the async word in the tag and it would fix the problem, but this is not a javascript file I am loading!

Is there anyway to keep this resource from blocking my browser and force it to wait until it is downloaded?

Flame_Phoenix
  • 16,489
  • 37
  • 131
  • 266
  • 1
    you can load the font at the end of the body using ... still, this would cause a FOUC, just like using JS. do you want them to wait a bit longer, or have flicker? personally, i'd wait... – dandavis Aug 10 '14 at 17:14
  • @dandavis: You're right, of course, although technically neither `style` nor `link` is valid inside `body` (or `html`, just `head`) (although the nightly spec has a `scoped` attribute that would allow it). But I don't think there's a browser in the world that cares. Doing it with JavaScript is probably not necessary. One might hope to start the download the **tiniest bit** sooner by throwing the JS in the `head`, but more likely the browser's prefetch scanner would find the `link` element just as soon anyway... – T.J. Crowder Aug 10 '14 at 19:49
  • @dandavis: Thank you, I will test that option. Personally I don't mind the FOUC, so I believe that is what I need! – Flame_Phoenix Aug 10 '14 at 19:53
  • @dandavis: FWIW, I'd post that as a answer (referencing the new [`scoped` attribute](http://www.w3.org/html/wg/drafts/html/master/document-metadata.html#the-style-element)). – T.J. Crowder Aug 10 '14 at 20:07
  • Got it working. Who is going to post the answer? xD – Flame_Phoenix Aug 10 '14 at 20:19
  • @T.J.Crowder: style is valid in body in HTML5. go ahead and change or append your answer if it helps. – dandavis Aug 10 '14 at 21:32
  • @dandavis: See above: **Only** with the `scoped` attribute, and that hasn't even made it past the nightlies yet. – T.J. Crowder Aug 10 '14 at 21:48

2 Answers2

19

You can do it with JavaScript:

<script>
(function() {
    var link = document.createElement('link');
    link.rel = "stylesheet";
    link.href = "//fonts.googleapis.com/css?family=Open+Sans:400,700";
    document.querySelector("head").appendChild(link);
})();
</script>

The font will be loaded out-of-band with the main rendering. Of course, that means there will be a visual change when the font finishes loading...and if the user has JavaScript disabled, it won't load the font at all.


Or, as dandavis points out, you could just use a style element at the end of body, just before the closing </body> tag:

<style>
@import "//fonts.googleapis.com/css?family=Open+Sans:400,700"
</style>

That's valid HTML now (as of the 20170808 draft of HTML 5.2), but I'd never met a browser that cared about it if you placed style in body even before it was made valid.

The advantages to this over using JavaScript are:

  1. In theory, the browser's prefetch scanner might find the style element and start the download earlier (although this isn't particularly likely if you put the JavaScript in head), and

  2. It works even if the user has JavaScript disabled.

Alternately, you could just move your link element to the end of body, but at present, that's invalid and the scoped attribute doesn't (yet?) seem to apply. (Why make it apply to style and not link[rel=stylesheet]? I have no idea, and perhaps it's simply a matter of not having got there yet...)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Comparing this solution to dandavis, what is the difference? His solution will still work if JS is not enabled right? And this one will not. What is the main advantage of this solution? – Flame_Phoenix Aug 10 '14 at 19:55
  • @Flame_Phoenix: Actually I think I'd probably go with a slight modification to Dan's: Put the `link` element at the end of `body`. It's not valid HTML, but I don't think there's a browser on the planet that would actually care. Or use a `style` element; at the *moment*, that would also be invalid, but if you throw a `scoped` attribute on it, apparently it [will be valid fairly soon](http://www.w3.org/html/wg/drafts/html/master/document-metadata.html#the-style-element). (No, I don't know why they would add `scoped` to `style` and not to `link[rel=stylesheet]`. Maybe it's just "not yet".) – T.J. Crowder Aug 10 '14 at 20:06
  • If you add that as an answer, I will gives kudos++ and accept it :D – Flame_Phoenix Aug 10 '14 at 20:20
  • @Flame_Phoenix: If Dan doesn't, you should go ahead and post what you did as an answer and (when SO lets you) accept it, being sure to credit Dan (and if relevant, myself). Glad to have been helpful! And I learned something, which is always great. – T.J. Crowder Aug 10 '14 at 20:56
  • 1
    @Flame_Phoenix: dandavis doesn't seem to want to post an answer, so I've updated the above. – T.J. Crowder Aug 10 '14 at 21:53
  • any idea for AMP pages? – kplshrm7 Apr 20 '17 at 11:27
  • @kplshrm7: I don't even know what an AMP page *is*. :-) – T.J. Crowder Apr 20 '17 at 11:59
  • 1
    @T.J.Crowder This is valid HTML [as of HTML 5.2](http://w3c.github.io/html/document-metadata.html#the-style-element) – syntagma Sep 18 '18 at 22:47
  • @syntagma - Thanks, looks like it changed between the [20170718](https://www.w3.org/TR/2017/WD-html52-20170718/) and [20170808](https://www.w3.org/TR/2017/CR-html52-20170808/) drafts. Finally! Not, again, that any browser ever enforced the previous rule. :-) – T.J. Crowder Sep 19 '18 at 06:43
0
<script src="https://ajax.googleapis.com/ajax/libs/webfont/1.5.18/webfont.js"></script>
<script>
 WebFont.load({
    google: {
      families: ['Open Sans:400,700,400italic,700italic']
    }
  });
</script>
tsogtgerel.ts
  • 955
  • 1
  • 15
  • 32
  • 4
    **From review queue**: May I request you to please add some more context around your answer. Code-only answers are difficult to understand. It will help the asker and future readers both if you can add more information in your post. – RBT May 13 '17 at 09:44