I want to have a series of the same SVG file on a page with different colours. I\'m aware that the best method of getting the SVG into the page without bloating the code, an
document.addEventListener("DOMContentLoaded", function() {
//attribute name
const ATTR_NAME = "data-src";
//base64 encoded brocken image icon
const ERROR_PLACEHOLDER =
"
";
let target = document.querySelectorAll(`[${ATTR_NAME}]`);
//unsorted list
let _filesList = [];
target.forEach(function(item) {
_filesList.push(item.getAttribute(ATTR_NAME).replace(/\\/g, "/"));
});
//sorted list
let filesList = _filesList.filter(function(item, pos) {
return _filesList.indexOf(item) == pos;
});
//ajax request
filesList.forEach(function(item) {
let ajax = new XMLHttpRequest();
ajax.open("GET", item, true);
ajax.onload = function() {
document.querySelectorAll(`[${ATTR_NAME}="${item}"]`).forEach(item => {
if (this.status >= 200 && this.status < 400) {
// Success!
item.innerHTML = this.response;
} else {
// Error!
item.innerHTML = ERROR_PLACEHOLDER;
}
});
};
ajax.send();
});
});
.icon path{
fill:#000;
}