Skip to content

Instantly share code, notes, and snippets.

@howardr
Created February 23, 2009 23:56
Show Gist options
  • Save howardr/69276 to your computer and use it in GitHub Desktop.
Save howardr/69276 to your computer and use it in GitHub Desktop.
fontAvailable jQuery plugin v1.1
/* fontAvailable jQuery Plugin, v1.1
*
* Copyright (c) 2009, Howard Rauscher
* Licensed under the MIT License
*/
(function($) {
var element;
$.fontAvailable = function(fontName) {
var width, height;
// prepare element, and append to DOM
if(!element) {
element = $( document.createElement( 'span' ))
.css( 'visibility', 'hidden' )
.css( 'position', 'absolute' )
.css( 'top', '-10000px' )
.css( 'left', '-10000px' )
.html( 'abcdefghijklmnopqrstuvwxyz' )
.appendTo( document.body );
}
// get the width/height of element after applying a fake font
width = element
.css('font-family', '__FAKEFONT__')
.width();
height = element.height();
// set test font
element.css('font-family', fontName);
return width !== element.width() || height !== element.height();
}
})(jQuery);
@heliocola
Copy link

Have you noticed that it doesn't work for 'Times New Roman'?
Somehow it was returning false to me so I started looking for the implementation and noticed that either for FAKEFONT and 'Times New Roman' the width is the same.
I tested on both Chrome and Firefox!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment