This question is related to a previous post: Check if user is using browser located in China or not
I am loading an external js library to evaluate which css do I want to load in my header. Following the accepted answer of the above mentionned post, I load the userinfo.io API in my HTML head, followed by the evaluation of the received data. Here is what I did:
<head>
...
<script type="text/javascript" src="//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 fallback fonts
document.write('<link href="http://fonts.useso.com/css?family=Source+Sans+Pro:200,400,700|Dancing+Script:400,700|PT+Serif|Open+Sans:400,300,700" rel="stylesheet" type="text/css">');
} else {
// Load google fonts
document.write('<link href="http://fonts.googleapis.com/css?family=Source+Sans+Pro:200,400,700|Dancing+Script:400,700|PT+Serif|Open+Sans:400,300,700" rel="stylesheet" type="text/css">');
}
}, function(err) {
// the "err" object contains useful information in case of an error
});
</script>
</head>
Debugging the js in Firefox, I can see that the library is successfully being loaded. It first happened, that I got an error message "Reference Error: UserInfo not defined". I played a bit with the order of the lines in my html head (there are a few more css includes, some meta tags and the title of course). After putting the
<head>
<script type="text/javascript" src="//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 fallback fonts
document.write('<link href="http://fonts.useso.com/css?family=Source+Sans+Pro:200,400,700|Dancing+Script:400,700|PT+Serif|Open+Sans:400,300,700" rel="stylesheet" type="text/css">');
} else {
// Load google fonts
document.write('<link href="http://fonts.googleapis.com/css?family=Source+Sans+Pro:200,400,700|Dancing+Script:400,700|PT+Serif|Open+Sans:400,300,700" rel="stylesheet" type="text/css">');
}
}, function(err) {
// the "err" object contains useful information in case of an error
});
</script>
</head>
as the first line inside the head tag, it started to actually recognize the object UserInfo
and its method getInfo()
. Meanwhile it also recognizes the function when I put the line back to it's original place (such as in the first code snippet). This behavior somehow made me wonder on how is javascript being executed in the head tag while loading the html page. Is it possible that once in a while the method is being called before the library is loaded?
Anyway, now when it successfully imports the userinfo.io library, the site does not load properly anymore. Having a look at the browser console I see:
Having a look at the source code of my page I only see the link tag that is properly loaded, but the rest of the page is missing.
So here's what I suppose:
I can somehow not use document.write()
in the head tag because it interferes with the page loading. More likely I should access and modify the link tag by getElementById()
after the page has successfully loaded. So I will actually trigger this with let's say jQuery's ready()
function to be sure that the library has successfully loaded? I would appreciate any comment on these thoughts.