Font-Face changing via JavaScript
You can create a new <style>
element with the @font-face
rule and append it to the document
's head
:
var newStyle = document.createElement('style');
newStyle.appendChild(document.createTextNode("\
@font-face {\
font-family: " + yourFontName + ";\
src: url('" + yourFontURL + "') format('yourFontFormat');\
}\
"));
document.head.appendChild(newStyle);
Of course, you'll probably need to provide all the necessary font formats and URLs, too, unless you're only worried about support for modern desktop browsers (in which case you would just use WOFF – I assume that's reasonable, because of the other features you mentioned).
Define a FontFace object:
new_font = new FontFace('conthrax', 'url(fonts/conthrax-sb.ttf)')
Call its load method to download the font:
new_font.load().then(function(loaded_face) {
// use font here
}).catch(function(error) {
});
... this returns a Promise, which when resolved passes the loaded FontFace.
Add the loaded font to the document:
new_font.load().then(function(loaded_face) {
// use font here
document.fonts.add(loaded_face)
}).catch(function(error) {
});