Skip to content

Instantly share code, notes, and snippets.

@stowball
Created October 6, 2015 03:31
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stowball/098ee0c0776e1b993411 to your computer and use it in GitHub Desktop.
Save stowball/098ee0c0776e1b993411 to your computer and use it in GitHub Desktop.
A lightweight, asynchronous SVG icon system loader
<script>
(function (url) {
var id = 'svg-sprite';
var container;
var xhr = new XMLHttpRequest();
var body = document.body;
if ('withCredentials' in xhr) {
xhr.withCredentials;
xhr.open('GET', url, true);
} else if (typeof XDomainRequest != 'undefined') {
xhr = new XDomainRequest();
xhr.open('GET', url);
} else {
body.className += ' no-svg-sprite';
return;
}
xhr.onload = function () {
container = document.createElement('div');
container.id = id;
container.innerHTML = xhr.responseText;
body.insertBefore(container, body.childNodes[0]);
};
xhr.onerror = function () {
body.className += ' no-svg-sprite';
};
setTimeout(function () {
xhr.send();
}, 0);
})('http://path-to/sprite.svg');
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment