Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if font awesome is blocked from a client's browser?

First of all, there is a very similar question here:
How to check if Font Awesome is loaded in web page with javascript?

I will try to explain why my question is different.

I have discovered that browsers have started to block web fonts in general, and this calls for fallback solutions. With normal character-range fonts, this is easy:

CSS:

font-family: Lato, "Lucida Grande", Tahoma, Sans-Serif;

Font awesome however is harder, as it uses characters outside the normal range of fonts, and cannot have a simple one line css solution.

There are several options available, including using images, or in some cases characters from other fonts, similar to an icon, but all approaches needs a way to check if font awesome is actually working or not, in the individual client's browser.

The answer from the linked question says that you can check if font awesome is loaded, by using this code:

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);
};

However, my local fallback is never executed, as the css(span, 'font-family') does in fact return FontAwesome. I believe this is because the css file is imported without problems, and the css says the font family should be FontAwesome. The fact still remains that FontAwesome is not working. In other words, the accepted answer checks simply that font awesome css exist, and not if the web font itself is blocked due to security concerns.

The OP of the other question clarifies in comments why they asked the question:

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.

My problem is browsers blocking web fonts, and I hope the title is clear enough to be considered a separat and different question. The problem exists in the combined setup of Windows 10 and IE11. I have exhausted my options to simply turn off the security settings blocking the font.

Duplicate question explanation:

The question is not at all a duplicate of the suggested question. This is not about if the resource is loaded or not. The loading of the resources works just fine. The hosts are not blocked. The rendering and display of the webapps is the problem, which is also explained in the original question, but mentioned again now, as someone suggested to close as a duplicate.

EDIT, FURTHER READING:

My efforts to turn off the settings blocking the web fonts are explained here

like image 568
jumps4fun Avatar asked Feb 07 '19 16:02

jumps4fun


People also ask

How do I know if Font Awesome is loaded?

With Font Awesome there's no JavaScript to detect. One way we can detect Font Awesome is by creating an element and checking if the correct CSS is applied to it.

Why is Font Awesome not working?

Make sure you're using the latest and greatest by updating your CDN code reference, updating your Font Awesome package via npm, or downloading a fresh copy of Font Awesome. You can check with version an icon was added to on its detail page (e.g. question-circle was added in Verion 1 but last updated in 5.0.

Can I use Font Awesome for commercial use?

Font Awesome is fully open source and is GPL friendly. You can use it for commercial projects, open source projects, or really just about whatever you want.

Why can't I see Font Awesome icons?

If you installed the Free Font Awesome version but try adding Pro icons to your web pages, they won't show. The solution to this is either you use the alternative free icons or upgrade to a Pro subscription/license.


1 Answers

The solution you're using will not work because window.getComputedStyle will not tell you about the rendered font but the font defined in the stylesheet, loaded or otherwise (ref). You could use heuristics:

window.onload = function() {
  var fas = document.createElement("span");
  fas.className = "fas fa-ambulance";
  fas.style.position = "absolute";
  fas.style.left = 0;
  fas.style.top = 0;
  fas.style.visibility = "hidden";
  document.body.appendChild(fas);
  window.setTimeout(function() {
    var widthBefore = fas.offsetWidth;
    fas.style.fontFamily = "asdf";
    var widthAfter = fas.offsetWidth;
    if (widthBefore === widthAfter) {
      console.log("Font Awesome Blocked");
    } else {
      console.log("Font Awesome Loaded");
    }
    document.body.removeChild(fas);
  }, 2000);
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.1/css/all.css">

The above creates an element that uses Font Awesome and compares its width with an element that is forced to use a fallback font. The width comparison will tell you if the browser used a fallback font for Font Awesome as well.

like image 184
Salman A Avatar answered Sep 29 '22 17:09

Salman A