Last active
September 19, 2015 12:32
-
-
Save sampotts/13d1cf84ca149d9f8871 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ========================================================================== | |
// SVG sprite loading | |
// This file should be at the top of the body to avoid a flash | |
// ========================================================================== | |
// Loading SVG in with AJAX | |
// --------------------------------- | |
(function(url) { | |
var id = "svg-sprite", | |
cb = (window.location.host.indexOf("local.") === 0); | |
// Cache busting locally | |
if(cb) { | |
url += "?" + Math.floor(Math.random() * 1001); | |
} | |
// Respect protocol for IE9 | |
// http://stackoverflow.com/a/8509981/1191319 | |
if(window.location.protocol === "http:") { | |
url = url.replace("https", "http"); | |
} | |
// Only load once | |
if(document.querySelectorAll("#" + id).length === 0) { | |
var xhr = new XMLHttpRequest(), | |
body = document.body; | |
// Create container | |
var container = document.createElement("div"); | |
container.setAttribute("hidden", ""); | |
container.setAttribute("id", id); | |
// XHR for Chrome/Firefox/Opera/Safari | |
if ("withCredentials" in xhr) { | |
xhr.open("GET", url, true); | |
} | |
// XDomainRequest for older IE | |
else if(typeof XDomainRequest != "undefined") { | |
xhr = new XDomainRequest(); | |
xhr.open("GET", url); | |
} | |
// Not supported | |
else { | |
return; | |
} | |
// Once loaded, inject to container and body | |
xhr.onload = function() { | |
container.innerHTML = xhr.responseText; | |
// Inejct the SVG to the body | |
body.insertBefore(container, body.childNodes[0]); | |
}; | |
// Timeout for IE9 | |
setTimeout(function () { | |
xhr.send(); | |
}, 0); | |
} | |
})(config.cdn.assets + "/sprite.svg"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment