Skip to main content

SVG polyfill in pure Javascript

Reading time: 1 minute Suggest an edit

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'));