views:

54

answers:

4

Hi,

How to detect whether or not a particular font is installed using javascript only. (Disregard to whether it is enabled or not).

Thanks

+4  A: 

How about you just do what the rest of the world does: specify the font with CSS, and offer fallbacks? No app should ever need to know what fonts are available, nor is there a reliable way of doing so. (You would probably have to print hidden div with the font and measure it to see if the dimensions are what you expected, but that would be extremely brittle.)

Just go for:

body { font-family: MyFont, Helvetica, Arial, sans-serif }

If you want to be doing anything with the font other than display things in it if possible, consider an alternative solution.

Matchu
+1  A: 

@Matchu suggested you rightly but here is what you are looking for:

http://remysharp.com/2008/07/08/how-to-detect-if-a-font-is-installed-only-using-javascript/

Sarfraz
That solution won't work in all cases. It relies on a particular font being installed, but that won't always be true.
Pointy
@Pointy: Won't work also in case javascript is disabled.
Sarfraz
Well yes there's that too! :-)
Pointy
A: 

Font family declarations should suffice to provide a fallback in case the font you want to use is not present.

But if you really need to use a special font in your site, although this uses JavaScript, I'd recommend Cufon since it's the most cross-browser solution out there - there is a CSS3 fix (font-face declarations), but it doesn't work in all browsers yet.

Seb
CSS font declarations work everywhere, including IE6.
Pointy
@Pointy No, font-face won't. It's a CSS3 only feature.
Seb
@Seb That is incorrect, sir. http://webfonts.info/wiki/index.php?title=%40font-face_support_in_Internet_Explorer
Pointy
@Pointy what's that link supposed to mean? That a single browser supports any given feature doesn't mean it's from one or other technology! font-face *is* CSS3, in spite of which browsers support it. What I was saying is that, while it may work on IE, it won't work *everywhere*, as you happily alleged.
Seb
Ah, well OK that makes sense. However, to my knowledge it works in the majority of browsers in use where it makes a difference (i.e., it doesn't work in lynx, but then the basic problem here is irrelevant there too). I guess the biggest hole is pre-3.5 Firefox, right?
Pointy
... oh, and the iPhoo devices right?
Pointy
A: 

If you absolutely need a specific font, all your visitors must have current browsers. Then you can use webfonts.

If that's not an option, you must render the font on your server and serve an image.

Aaron Digulla
IE6 supports downloaded fonts.
Pointy