Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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