Skip to main content
I'm streaming on Twitch right now. You should stop by. Let's go!

SVG polyfill in pure Javascript


A designer at work pointed out this SVG polyfill as a candidate for our new production site. While I like the elegance of it, I was a bit disappointed that the author felt the need to include jQuery as a dependency simply to use its selector engine and each function. So, I rewrote it in pure Javascript.


if(!(!!document.createElementNS && !!document.createElementNS('', "svg").createSVGRect)){
    var imgs = document.getElementsByTagName('img');

    for(var i = 0; i < imgs.length; i++) {
        imgs[i].setAttribute('src', imgs[i].getAttribute('src').replace(/\.svg/, '.png'));