I need to check if a web page has Font Awesome in it. If not I'm going to load it with javascript. Kinda like how the facebook sdk checks to see if there is a script element containing the id "facebook-jssdk", if so it just returns (does nothing), if not it loads it. I need to do that for Font Awesome.
Asked
Active
Viewed 1.1k times
6
-
1`!!Array.prototype.filter.call(document.styleSheets, function(ss) { return ss.href.indexOf('fontawesome') > -1 }).length`? – haim770 Aug 01 '16 at 14:36
-
1Why the down vote? – cmac Aug 01 '16 at 14:37
-
do you want to check the style or the script – tsadkan yitbarek Aug 01 '16 at 14:37
-
The stylesheet, I didn't know font awesome had a script file that goes with it. I've always just loaded the stylesheet. @tsadkanyitbarek – cmac Aug 01 '16 at 14:41
-
sorry i update my answer. I thought he needs to check if a script is loaded. – tsadkan yitbarek Aug 01 '16 at 14:42
-
@haim770 neat, but it needs `ss.href && ss.href.indexOf('fontawesome') > -1` in the case of embedded stylesheets (which results in a null href attribute). You can test it out right here on SO. – Joseph Marikle Aug 01 '16 at 14:42
-
@JosephMarikle. True. – haim770 Aug 01 '16 at 14:43
-
@Archer I would do that, but I don't have full control over the pages that the script is gonna go into, it needs to load the things it needs. Some of the pages my have font awesome, some might not. If I built the pages, then I would know, but my script is gonna just be added on to the end, so it has to check and load everything it's self, jquery, font awesome, sdks, etc... – cmac Aug 01 '16 at 14:44
-
Sorry I update my answer. I thought he needed to check if script is loaded. – tsadkan yitbarek Aug 01 '16 at 14:45
-
1http://stackoverflow.com/a/33651424/27754 could help. I have a feeling that you may end up putting more effort into finding whether it is loaded rather that just potentially including it twice though. – ca8msm Aug 01 '16 at 14:58
-
@ca8msm I think your right. Nick Bull said the same thing, I'm gonna go ahead and just load it. Thanks! – cmac Aug 01 '16 at 15:01
-
Why all the down votes? SMH – cmac Aug 01 '16 at 16:00
4 Answers
6
I think this is the best way to check for font-awesome, but I'm not sure if it's slower then just loading it again even if it is there.
function css(element, property) {
return window.getComputedStyle(element, null).getPropertyValue(property);
}
window.onload = function () {
var span = document.createElement('span');
span.className = 'fa';
span.style.display = 'none';
document.body.insertBefore(span, document.body.firstChild);
if ((css(span, 'font-family')) !== 'FontAwesome') {
// add a local fallback
}
document.body.removeChild(span);
};

cmac
- 3,123
- 6
- 36
- 49
3
One, not sure-fire way, would be to check for the existence of the Css file. The function below would find many variations, such as ones served from a cdn, or have a "-min" suffix in the file name. There are unlikely instances this will falsely return true (if for instance somebody created an additional css file called "font-awesome-extensions.css"). A more likely problem with this approach is if a font-awesome is bundled in another file then this wouldn't find the file and falsely return false.
function findCss(fileName) {
var finderRe = new RegExp(fileName + '.*?\.css', "i");
var linkElems = document.getElementsByTagName("link");
for (var i = 0, il = linkElems.length; i < il; i++) {
if (linkElems[i].href && finderRe.test(linkElems[i].href)) {
return true;
}
}
return false;
}
console.log(findCss("font-awesome"));

Daniel Gimenez
- 18,530
- 3
- 50
- 70
1
Here's a way, but ask yourself why you're doing this? source.
/**
* Checks if a font is available to be used on a web page.
*
* @param {String} fontName The name of the font to check
* @return {Boolean}
* @license MIT
* @copyright Sam Clarke 2013
* @author Sam Clarke <sam@samclarke.com>
*/
(function (document) {
var calculateWidth, monoWidth, serifWidth, sansWidth, width;
var body = document.body;
var container = document.createElement('div');
var containerCss = [
'position:absolute',
'width:auto',
'font-size:128px',
'left:-99999px'
];
// Create a span element to contain the test text.
// Use innerHTML instead of createElement as it's smaller
container.innerHTML = '<span style="' + containerCss.join(' !important;') + '">' +
Array(100).join('wi') +
'</span>';
container = container.firstChild;
calculateWidth = function (fontFamily) {
container.style.fontFamily = fontFamily;
body.appendChild(container);
width = container.clientWidth;
body.removeChild(container);
return width;
};
// Pre calculate the widths of monospace, serif & sans-serif
// to improve performance.
monoWidth = calculateWidth('monospace');
serifWidth = calculateWidth('serif');
sansWidth = calculateWidth('sans-serif');
window.isFontAvailable = function (fontName) {
return monoWidth !== calculateWidth(fontName + ',monospace') ||
sansWidth !== calculateWidth(fontName + ',sans-serif') ||
serifWidth !== calculateWidth(fontName + ',serif');
};
})(document);

Nick Bull
- 9,518
- 6
- 36
- 58
-
I'm doing it because I'm writing a snippet of js that is going to be loaded in many different pages that I'm not sure are all gonna have font-awesome, if they don't I'm going to load it. – cmac Aug 01 '16 at 14:38
-
1Why not just try to load it using CSS? If it's for performance then you're going to use as many resources testing the font exists as you are attempting to load it, if not significantly more. – Nick Bull Aug 01 '16 at 14:40
-
And to the downvoter, this function works. And answers the problem. Why downvote? – Nick Bull Aug 01 '16 at 14:40
-
I think you might be right, I should just load it anyways, huh? @Nick Bull – cmac Aug 01 '16 at 14:49
-
1
0
You could do like this with jquery
var url = "https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css";
check if the css is loaded
if (!$("link[href=url]").length)
//append the link to the head tag if not loaded
$('<link href=url rel="stylesheet">').appendTo("head")

tsadkan yitbarek
- 1,360
- 2
- 11
- 28
-
The problem with this is Font Awesome could be loaded from cdn or from other server. I don't necessarily know the url of font awesome if it is loaded. – cmac Aug 01 '16 at 14:47
-
if it is loaded $("link[href=url]").length returns greater than 0. – tsadkan yitbarek Aug 01 '16 at 14:51
-
idk... I don't think your answer deserved a down vote either. I believe it's helpful even if it ain't the solution @tsadkan – cmac Aug 01 '16 at 15:03
-
1`$("link[href=url]")` ?? Dontcha mean `$("link[href="+url+"]")` which still only checks that specific url – Brad Kent Nov 18 '16 at 22:24