问题
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.
回答1:
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);
};
回答2:
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"));
回答3:
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);
回答4:
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")
来源:https://stackoverflow.com/questions/38701497/how-to-check-if-font-awesome-is-loaded-in-web-page-with-javascript