Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import font file using JavaScript?

CSS has a @font-face rule which allows us to "use" custom fonts.

Does JavaScript have this functionality?

More specifically, is it possible to "use" custom fonts without using the CSS @font-face rule?

like image 439
Pacerier Avatar asked Apr 07 '11 19:04

Pacerier


2 Answers

if you know CSS you can use document.write to append a style sheet dynamically... I guess that would count as using CSS, but it would work

like image 106
Nate Koppenhaver Avatar answered Sep 25 '22 18:09

Nate Koppenhaver


There is no way to cause the browser to load a font from the network other than with a @font-face rule. You could theoretically create that @font-face rule from JavaScript without having to use document.write (or document.createElement("style") etc), if instead you used the CSS Object Model; but I wouldn't count on that to be implemented in any browser at the moment.

like image 45
zwol Avatar answered Sep 23 '22 18:09

zwol