Trying to load a font with the help of WebFontLoader, I encountered an issue where setting an element's font using
document.getElementById("my-button").style.fontFamily = "Font I Want"
doesn't seem to work as expected. What makes this situation even stranger is:
- I can manually change the font to "Font I Want" by directly applying the font-family rule to the element in the browser's dev tools.
- I am able to set common fonts like Arial using
via the console in dev tools, but for some reason, setting it to "Font I Want" has no effect.document.getElementById("my-button").style.fontFamily = "Arial"
- Interestingly, when attempting to apply a misspelled font through JavaScript, the element reverts to the default system font rather than throwing an error, whereas "Font I Want" does not get applied at all - puzzling behavior that suggests the browser recognizes the custom font but refuses to implement it.
Despite these challenges, manually setting the font using CSS works perfectly fine, confirming that the font is indeed loaded correctly.
What could be causing JavaScript to struggle with setting the style.fontFamily
property dynamically after loading a font? Is there a way to resolve this issue without relying on CSS due to the dynamic nature of the font loading process from the server?