Detecting Fonts Ready

By  on  

Knowing when resources are loaded is a key part of building functional, elegant websites. We're used to using the DOMContentLoaded event (commonly referred to as "domready") but did you know there's an event that tells you when all fonts have loaded? Let's learn how to use document.fonts!

The document.fonts object features a ready property which is a Promise representing if fonts have been loaded:

// Await all fonts being loaded
await document.fonts.ready;

// Now do something!  Maybe add a class to the body
document.body.classList.add('fonts-loaded');

Font files can be relatively large so you can never assume they've loaded quickly. One simply await from document.fonts.ready gives you the answer!

Recent Features

  • By
    Convert XML to JSON with JavaScript

    If you follow me on Twitter, you know that I've been working on a super top secret mobile application using Appcelerator Titanium.  The experience has been great:  using JavaScript to create easy to write, easy to test, native mobile apps has been fun.  My...

  • By
    Welcome to My New Office

    My first professional web development was at a small print shop where I sat in a windowless cubical all day. I suffered that boxed in environment for almost five years before I was able to find a remote job where I worked from home. The first...

Incredible Demos

Discussion

    Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!