Skip to content

Instantly share code, notes, and snippets.

@jpsantos7
Forked from motrdevua/inlineSvg.js
Created April 21, 2021 17:55
Show Gist options
  • Save jpsantos7/33be4bea05334aacb72b65d2c6004acd to your computer and use it in GitHub Desktop.
Save jpsantos7/33be4bea05334aacb72b65d2c6004acd to your computer and use it in GitHub Desktop.
inline svg from img src
/**
* Inline svg
*/
const allImg = document.querySelectorAll('img');
for (let i = 0; i < allImg.length; i++) {
const imgSrc = allImg[i].getAttribute('src');
if (imgSrc.split('.')[1] === 'svg') {
let myRequest = new Request(imgSrc);
fetch(myRequest)
.then((response) => {
return response.text();
})
.then((svg) => {
allImg[i].parentNode.innerHTML = svg;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment