Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know when font-face has been applied

I am currently building a corporate website for a customer that uses custom fonts extensively.

On jQuerys DOM-ready I am doing placement calculations to figure out where some pop-up menus with dynamic width and height should be placed based on their dynamic contents.

These calculations fail, since DOM-ready is fired before font-face is applied, and thus widths and heights are incorrect.

Right now (for the prototype) i am doing the calculations 500ms after DOM-ready to alleviate this problem, but this can't go into production for obvious reasons.

The problem has been observed in latest Firefox and chrome. IE 8 doesn't seem to have the problem, but then DOM-ready fires fairly late, so the delay is kind of built in I guess :)

Waiting for the load event is not an option, so my question to you is this:

Is there a reliable cross-browser way to detect when font-face has been applied?

like image 742
Martin Jespersen Avatar asked Jul 13 '11 10:07

Martin Jespersen


People also ask

How can I tell if font face is working?

Chrome / Microsoft Edge To see the rendered font you only have to navigate to “Elements”, select and element from the DOM, and select the “Computed” Style information. Scroll to the bottom, and you will see the font that gets rendered on this element.

What does font face mean in Word?

The @font-face CSS at-rule specifies a custom font with which to display text; the font can be loaded from either a remote server or a locally-installed font on the user's own computer.

How do I know if Font Awesome is loaded?

With Font Awesome there's no JavaScript to detect. One way we can detect Font Awesome is by creating an element and checking if the correct CSS is applied to it.


2 Answers

I found a solution after wondering why IE doesn't suffer from this problem.

Firefox and Chrome/Safari triggers the DOMContentLoaded event before font-face is applied, thus causing the problem.

The solution is to not listen for DOMContentLoaded but instead go oldschool and listen to onreadystatechange and wait until the document.readyState === 'complete' which is always triggered after font-face is applied (as far as I can tell by my tests) - which is of course what always happens in IE since it doesn't support DOMContentLoaded.

So basically you can roll-your-own event in jQuery called fontfaceapplied - maybe it should be built in ;)

document.onreadystatechange = function() {     if (document.readyState === 'complete')          $(document).trigger('fontfaceapplied'); }; 

Funny fact: Opera does it right and waits to trigger DOMContentLoaded until font-face is applied.

like image 167
Martin Jespersen Avatar answered Sep 19 '22 14:09

Martin Jespersen


ES6 update:

The question post is for many years ago which the IEs version 8 and earlier were still alive and even Ecmascript version 6 was not released, but now you can write callbacks on document.fonts events. eg:

document.fonts.onloadingdone = () => {   // do something after all fonts are loaded }; 

For more information see this post.

like image 44
AmerllicA Avatar answered Sep 16 '22 14:09

AmerllicA