Created
February 23, 2009 23:56
-
-
Save howardr/69276 to your computer and use it in GitHub Desktop.
fontAvailable jQuery plugin v1.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!