检查用户是否使用位于中国的浏览器

Fla*_*dre 3 html css fonts geolocation

自2014年以来,中国正在阻止Google API,这意味着包含Map API或Fonts API的网站的加载时间较长.有一种解决方法,因为fonts.useso.com也提供相同的字体库(分别参见libs.useso.com,fonts.useso.com).

以下代码

<link href='http://fonts.googleapis.com/css?family=Open+Sans:300,400,600&subset=latin,latin-ext' rel='stylesheet'>
Run Code Online (Sandbox Code Playgroud)

可以变成

<link href='http://fonts.useso.com/css?family=Open+Sans:300,400,600&subset=latin,latin-ext' rel='stylesheet'>
Run Code Online (Sandbox Code Playgroud)

它在中国会很好用.

但是,如果我想动态选择我想要加载API的源,考虑用户访问的位置,该怎么办?

Vin*_*ont 7

您可以使用第三方服务来确定用户位置并切换字体.

例如,您可以集成免费的userinfo.io.在这种情况下,位置是根据IP地址确定的,因此如果中国人从欧洲访问,他将位于欧洲并且将加载谷歌字体.

将此插入您的<head>:

<script type="text/javascript" href="//cdnjs.cloudflare.com/ajax/libs/userinfo/1.0.0/userinfo.min.js"></script>

<script type="text/javascript">
UserInfo.getInfo(function(data) {
  // the "data" object contains the info
  if (data.country.code == 'CN') {
    // load your fallback fonts
  } else {
    // Load your google fonts
  }
}, function(err) {
  // the "err" object contains useful information in case of an error
});
</script>
Run Code Online (Sandbox Code Playgroud)

免责声明:我编写并维护了userinfo.io