public
Last active

Font-face detection

  • Download Gist
fontface-detect-buggy.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
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';
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.