Created
October 6, 2015 03:31
-
-
Save stowball/098ee0c0776e1b993411 to your computer and use it in GitHub Desktop.
A lightweight, asynchronous SVG icon system loader
This file contains hidden or 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
<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