如何使用javascript检查是否在网页中加载了Font Awesome? [英] How to check if Font Awesome is loaded in web page with javascript?

查看:108
本文介绍了如何使用javascript检查是否在网页中加载了Font Awesome?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查一个网页是否包含Font Awesome。如果没有,我将用JavaScript加载它。有点像facebook sdk如何检查是否有一个包含idfacebook-jssdk的脚本元素,如果是这样它只返回(什么都不做),如果没有它加载它。我需要为Font Awesome做到这一点。

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.

推荐答案

我认为这是检查字体真棒的最好方法,但我我不确定它是否慢,然后再次加载即使它在那里。

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

这篇关于如何使用javascript检查是否在网页中加载了Font Awesome?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆