Skip to content

Instantly share code, notes, and snippets.

@radxene
Created October 30, 2016 09:39
Show Gist options
  • Save radxene/42ca772f40e6b0f8371efa1d5b4da3ee to your computer and use it in GitHub Desktop.
Save radxene/42ca772f40e6b0f8371efa1d5b4da3ee to your computer and use it in GitHub Desktop.
Insert inline svg in document from images src
window.addEventListener('load', function () {
'use strict';
var images = document.querySelectorAll("img[src$='.svg'");
var len = images.length;
for (var i = 0; i < len; i++) {
var img = images[i];
svgFromIMG(img);
}
function svgFromIMG(img) {
var id = img.id;
var url = img.getAttribute('src');
var classes = img.getAttribute('class');
var parent = img.parentNode;
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onload = function () {
var xml = xhr.responseXML;
if (!xml) { return }
var svg = xml.documentElement;
if (id) { svg.id = id }
if (classes) { svg.setAttribute('class', classes) }
parent.appendChild(svg);
parent.removeChild(img);
};
xhr.send();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment