Created
October 30, 2016 09:39
-
-
Save radxene/42ca772f40e6b0f8371efa1d5b4da3ee to your computer and use it in GitHub Desktop.
Insert inline svg in document from images src
This file contains 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
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