Check if string is html font in JavaScript
I am building a robotic talk and answer console and I want to be able to change the font in the console. I know how to change the font, but I don't want to change the font if the given font is not the actual HTML font.
Example: the user enters Arial
, the command passes. The user enters I lost my mommy
, the command fails, and the computer warns the user that the command is invalid.
I obviously don't want to create a very large array containing all the HTML fonts. This makes no sense.
If jQuery solution works then let me know. I am using jQuery in my code.
source to share
JavaScript / CSS Font Detector is a library that can help.
Include the library and call
detective.detect('[font name]'));
This is not a true font identifier, but it may work for what you are trying to accomplish.
detect
returns a boolean value based on the specified string value being in the font available in the browser.
function isFont(fontName) {
return fontName ? detective.detect(fontName) : false;
}
source to share