Skip to main content

img .svg to svg/xml in DOM

jQuery(document).ready(function () {
    document.querySelectorAll('.svghover_footer  img').forEach(function(img){
        var imgID = img.id;
        var imgClass = img.className;
        var imgURL = img.src;

        fetch(imgURL).then(function(response) {
            var d = response.text();
            var u = response.url;
            if(!d && acceptable[typeof d]){
            
            }else{
                if(u.indexOf('.svg') > -1){
                    return d;
                }else{
                    
                }
            }
            return false;
        }).then(function(text){
            if(text!=false){
                var parser = new DOMParser();
                var xmlDoc = parser.parseFromString(text, "text/xml");

                // Get the SVG tag, ignore the rest
                var svg = xmlDoc.getElementsByTagName('svg')[0];

                // Add replaced image's ID to the new SVG
                if(typeof imgID !== 'undefined') {
                    svg.setAttribute('id', imgID);
                }
                // Add replaced image's classes to the new SVG
                if(typeof imgClass !== 'undefined') {
                    svg.setAttribute('class', imgClass+' replaced-svg');
                }

                // Remove any invalid XML tags as per validator.w3.org


                //svg.removeAttribute('xmlns:a');

                // Check if the viewport is set, if the viewport is not set the SVG wont't scale.
                if(!svg.getAttribute('viewBox') && svg.getAttribute('height') && svg.getAttribute('width')) {
                    svg.setAttribute('viewBox', '0 0 ' + svg.getAttribute('height') + ' ' + svg.getAttribute('width'))
                }

                // Replace image with new SVG
                img.parentNode.replaceChild(svg, img);
        }
        });
            
    });
});

X

Wir verwenden Cookies

Wir nutzen Cookies auf unserer Website. Einige sind notwendig, während andere uns helfen, diese Website und Ihre Erfahrung zu verbessern.