Zeev Katz Zeev Katz - 1 year ago 124
Javascript Question

How to import fonts with pure JS?

I need to complete a site without using CSS at all.

Every thing goes right but i don't know how to change my font-family with pure JS.

Does anyone know how it is possible to import fonts using JS only?


Answer Source

I would suggest using Google's Webfontloader, it's incredibly simple to use:

    google: {
        families: ['Droid Sans', 'Droid Serif']

Which would allow you to use the massive web font library hosted by google here, without needing to explicitly specify the CSS. You can use custom fonts as well if you're willing to use a style tag or a .css file (or you can inject the @font-face via javascript.) There are other web font providers that work with the Webfontloader, just look on the github page.

If you're interested in learning how to do this manually in javascript, any query on injecting CSS via javascript will sort you, but, that's still technically using CSS. Not sure if this/that is what you want.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download