Skip to content

Instantly share code, notes, and snippets.

@kaelig
Created October 11, 2012 16:42
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaelig/3873753 to your computer and use it in GitHub Desktop.
Save kaelig/3873753 to your computer and use it in GitHub Desktop.
Font-face detection
var isFontFaceSupported = (function () {
if (!!navigator.userAgent.match(/(Android (2.0|2.1))|(Nokia)|(OSRE\/)|(Opera (Mini|Mobi))|(w(eb)?OSBrowser)|(UCWEB)|(Windows Phone)|(XBLWP)|(ZuneWP)/)) {
return false;
}
var sheet,
doc = document,
head = doc.head || doc.getElementsByTagName('head')[0] || docElement,
style = doc.createElement("style"),
impl = doc.implementation || { hasFeature: function () { return false; } };
style.type = 'text/css';
head.insertBefore(style, head.firstChild);
sheet = style.sheet || style.styleSheet;
var supportAtRule = impl.hasFeature('CSS2', '') ?
function (rule) {
if (!(sheet && rule)) { return false; }
var result = false;
try {
sheet.insertRule(rule, 0);
result = !(/unknown/i).test(sheet.cssRules[0].cssText);
sheet.deleteRule(sheet.cssRules.length - 1);
} catch (e) { }
return result;
} :
function (rule) {
if (!(sheet && rule)) { return false; }
sheet.cssText = rule;
return sheet.cssText.length !== 0 && !(/unknown/i).test(sheet.cssText) &&
sheet.cssText
.replace(/\r+|\n+/g, '')
.indexOf(rule.split(' ')[0]) === 0;
};
return supportAtRule('@font-face{font-family:"font";src:"font.ttf";}');
})();
if (isFontFaceSupported) {
document.getElementsByTagName('html')[0].className += ' ff';
}
@MoniqueHahnefeld
Copy link

thx for that! :) 👍

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