Skip to content

Instantly share code, notes, and snippets.

@kyleslattery
Created February 2, 2009 23:05
Show Gist options
  • Save kyleslattery/57160 to your computer and use it in GitHub Desktop.
Save kyleslattery/57160 to your computer and use it in GitHub Desktop.
/*
jQuery Font Detection Plugin by Kyle Slattery (http://kyleslattery.com)
Used to determine if a font is installed on a user's computer
Inspired by: http://remysharp.com/2008/07/08/how-to-detect-if-a-font-is-installed-only-using-javascript/
Use:
if($.fontInstalled('Helvetica')) {
// font is installed
} else {
// font is not installed
}
*/
$.fontInstalled = function(font) {
var test_string = 'mmmmmmmmmwwwwwww';
var test_font = '"Courier New", monospace';
var testbed = $('<div></div>').css({position: 'absolute', left: '-9999px', visibility: 'hidden'});
var control = $('<span></span>').text(test_string).css({fontSize: '50px', fontFamily: test_font}).appendTo(testbed);
var tester = $('<span></span>').text(test_string).css({fontSize: '50px', fontFamily: font + ', ' + test_font}).appendTo(testbed);
testbed.appendTo('body');
var installed = (control.width() != tester.width())
testbed.remove();
return installed;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment