JavaScript Mapping Library
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!
DOMContentLoaded
document.fonts
The document.fonts object features a ready property which is a Promise representing if fonts have been loaded:
ready
// 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!
await
document.fonts.ready
The post Detecting Fonts Ready appeared first on David Walsh Blog.
David Walsh Blog
You must be logged in to post a comment.
This site uses Akismet to reduce spam. Learn how your comment data is processed.