Last active
April 19, 2018 00:17
-
-
Save treetrum/6914cb21d49cb8be4684e06cd1b35e28 to your computer and use it in GitHub Desktop.
Automatically inline SVGs when using img tags.
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
'use strict';(function(){var a=new DOMParser,b=document.querySelectorAll('img[data-svg-inline]');b.forEach(function(c){var d=c.src,e=c.parentNode;fetch(d).then(function(f){return f.text()}).then(function(f){var g=a.parseFromString(f,'image/svg+xml'),h=g.documentElement;e.replaceChild(h,c)})})})(); |
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
(() => { | |
let parser = new DOMParser(); | |
let svgTags = document.querySelectorAll('img[data-svg-inline]'); | |
svgTags.forEach(svgTag => { | |
let imageURL = svgTag.src; | |
let imageParent = svgTag.parentNode; | |
fetch(imageURL) | |
.then(response => response.text()) | |
.then(svgData => { | |
var svgDocument = parser.parseFromString(svgData, 'image/svg+xml'); | |
let svgElement = svgDocument.documentElement; | |
imageParent.replaceChild(svgElement, svgTag); | |
}); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment