1window.addEventListener('load', svgElementConvertImgToSvg); 2function svgElementConvertImgToSvg() { 3 // Get all site images 4 const images = document.querySelectorAll('a > img[src$=".svg"]'); 5 6 images.forEach(img => { 7 const parentAnchor = img.parentElement; 8 9 fetch(img.src) 10 .then(response => response.text()) 11 .then(data => { 12 // Create the new SVG element 13 const parser = new DOMParser(); 14 const parsedSvg = parser.parseFromString(data, 'image/svg+xml'); 15 const svgElement = parsedSvg.documentElement; 16 17 // Remove all <script> tags 18 const scripts = svgElement.querySelectorAll('script'); 19 scripts.forEach(script => { 20 script.parentNode.removeChild(script); 21 }); 22 23 // Copy all attributes from the <img> to the <svg> 24 for (let i = 0; i < img.attributes.length; i++) { 25 const attr = img.attributes[i]; 26 if (attr.name !== 'src') { // Evitem copiar l'atribut 'src' 27 svgElement.setAttribute(attr.name, attr.value); 28 } 29 } 30 31 // Set the SVG element styles 32 svgElement.style.width = "100%"; 33 svgElement.style.maxWidth = "100%"; 34 svgElement.style.height = "auto"; 35 svgElement.style.display = "block"; 36 svgElement.setAttribute('preserveAspectRatio', 'xMidYMid meet'); 37 38 // Replace the <img> with the <svg> 39 parentAnchor.replaceWith(svgElement); 40 }) 41 .catch(error => { 42 console.error("Error carregant l'SVG:", error); 43 }); 44 }); 45}