10

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

Salman A
  • 262,204
  • 82
  • 430
  • 521
jumps4fun
  • 3,994
  • 10
  • 50
  • 96
  • This isn't a solution but rather more information that might help (and does offer some solutions depending on your use case) https://stackoverflow.com/questions/31745307/ie-11-sometimes-preventing-the-display-of-font-awesome-webfonts – imvain2 Feb 07 '19 at 17:08
  • Can't you do an axios call to font-awesome and see what response code you get back? – geoidesic Feb 07 '19 at 19:39
  • Possible duplicate of [How to detect when one or more JS/CSS library fail to load (e.g. CDN)?](https://stackoverflow.com/questions/30546795/how-to-detect-when-one-or-more-js-css-library-fail-to-load-e-g-cdn) – Heretic Monkey Feb 07 '19 at 22:39
  • @imvain2 thanks for the link.I had already read it. I was really not kidding when i wrote that I have exhausted my options to simply turn off the security settings. It is unfortunately not an option to edit the secutiry options in this case. But thanks anyway :) – jumps4fun Feb 08 '19 at 09:19
  • @HereticMonkey, it's not the same question. If you see the answer from drkunibar, and my response to it, you can see that the resources loads just fine. I did also mention this in my original question. The blocking is happening on a different level, and I am having trouble identifying exactly how, and how to programmatically detect it. Right now I can only visually see it when loading the page. – jumps4fun Feb 08 '19 at 09:38
  • @geoidesic, I didn't know what axios was, but its uses seems to be related to checking the loading of the resources, which again is not the problem I am facing. The browser seems to load the resources just fine, it just refuses to actually use them after loading. Which is also the only reason why this question is not a duplicate of several others. Still, I appreciate all of these suggestions. I am learning a lot of new stuff, even if I haven't found an anser to the original question yet. – jumps4fun Feb 08 '19 at 09:47
  • Does your code work if you replace `!== 'FontAwesome'` with `!== 'Font Awesome 5 Free'` – Salman A Feb 08 '19 at 10:49
  • Im using font awesome 4.7, so no. – jumps4fun Feb 08 '19 at 10:51
  • @KjetilNordin I mean type in the exact font name or use `css(span, 'font-family').test(/awesome/i) === false` – Salman A Feb 08 '19 at 10:54
  • I am pretty sure "FontAwesome" is in fact the exact font name in this case – jumps4fun Feb 08 '19 at 11:00
  • So this is mainly about users who have explicitly blocked web font (rendering) for security reasons? Those people are probably already used to seeing rectangles instead of icons on a lot of sites, so I am not sure this “problem” requires any actual fixing. If this _breaks_ your site or application, because without icons your links/menus/buttons/whatever lose meaning, then I’d say you probably rather did not use icons properly to begin with. A mere single glyph from some fancy font could hardly ever have done a proper job of conveying the meaning in that case in the first place. – 04FS Feb 08 '19 at 11:11
  • All valid points which I'm already aware of. As I mentioned, this is a problem which has arisen through roll out of new setups, namely the combination of IE11 and Windows 10 at my work place. At this point I can not get the security responsible people to confirm if this is a permanent new setting that I will need to adjust to, or if it is a temporary error that they will find a work around for. The reason I am asking the question, is because I need a quick fix way to fix the major errors that happens when icon based buttons disappear. That, plus technical curiousity. – jumps4fun Feb 08 '19 at 11:40

2 Answers2

2

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.

Salman A
  • 262,204
  • 82
  • 430
  • 521
  • YES! This worked. I had to change the classname fas` to `fa` to make it work with my version of font awesome, 4.7. I tested the code in both blocked and unblocked mode, and it works! Thanks! – jumps4fun Feb 08 '19 at 12:01
0

On your page you can use the onload and onerror event

Example

<link rel="stylesheet" 
      href="https://use.fontawesome.com/releases/v5.7.1/css/all.css"
      onload="alert('everythings looks fine')" 
      onerror="alert('error while loading fontawesome');">

Inside the event you can set a global variable.

UPDATED

If you want to know if the font works, you can check, if the font is used:

<html>
  <head>
    <link rel="stylesheet"
          href="https://use.fontawesome.com/releases/v5.7.1/css/all.css"/>
    <script>
      function check(){
        console.log(window.getComputedStyle(document.getElementById('foo')).getPropertyValue("font-family"));
      }
    </script>
  </head>
  <body onload="check();"/>
     <span id="foo" class="fa"></span>
  </body>
</html>

In this example you see 'Font Awesome 5 Free' in the console if Font Awesone works

drkunibar
  • 1,327
  • 1
  • 7
  • 7
  • Thanks for the answer, but the css loads just fine into the browser, but is subsequently blocked. I tried this anyway, just to be sure, but as I expected, it simply alerts "everything looks fine", and font awesome is still blocked. So again, it is not the resource, but the web font which is blocked. – jumps4fun Feb 08 '19 at 09:28
  • blocked by what? A browser plugin? – Alnitak Feb 08 '19 at 09:35
  • I honestly don't know exactly what, but it seems by microsofts "new and improved" security settings. – jumps4fun Feb 08 '19 at 09:39
  • I've update my example.With the update you can check, if Font Awesome works. I couldn't reproduce yout behavoir, but I hope the update works for you – drkunibar Feb 08 '19 at 10:37
  • Your example is pretty much identical to the one already in my question, which does not work, unfortunately. Thanks again for the attempt, but it does not get me anywhere this time. – jumps4fun Feb 08 '19 at 10:41
  • By the way, I was able to reproduce the error on my own computer, in IE11, by clicking in to internet settings -> security -> user defined level -> and then selecting download of fonts "deactivate" towards the bottom of the list of options. The path texts are manually translated from danish, and may differ from actual english texts. – jumps4fun Feb 08 '19 at 10:46