I'm attempting to create a local fallback for font-awesome, and I'm having some issues adding the <link>
tag to the <head>
element when the CDN fails to load.
Expected Behavior
If the GET request to the CDN produces a status other than 200 upon page load, a link to the local version of font-awesome should be added to the <head>
element.
Actual Behavior
A failed GET request to the CDN doesn't add a link to the local version of font-awesome to the <head>
element.
The issue appears to be happening within the code block that starts with xmlHttp.onreadystatechange = function() {
.
When I move parentElement.insertBefore(createFallback, referenceElement);
outside of the xmlHttp.onreadystatechange = function() {
block, the link to the local fallback gets added to the <head>
element just fine. But I don't understand why it isn't working within the code block.
I'd like to be able to detect whether the CDN is down, and if so, apply a link to the local version of font-awesome. I'm not receiving any error messages in the console while viewing the page,
My code is below:
<!doctype html>
<html class="no-js" lang="">
<head>
<link id="fontAwesomeFallback" rel="stylesheet" href="#">
<script>
// Define Variables
var createFallback = document.createElement("link");
createFallback.type = "text/css";
createFallback.rel = "stylesheet";
createFallback.href = "/css/font-awesome.css";
var referenceElement = document.getElementById("fontAwesomeFallback");
var parentElement = referenceElement.parentNode;
// Open Http Request
xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.3.0/css/font-awesome.min.css", true);
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status !== 200) {
parentElement.insertBefore(createFallback, referenceElement);
console.log("Local fallback appended to head element");
}
}
</script>
</head>
<body>
</body>
</html>
I'd really appreciate any help you all can offer!
Thanks!