Skip to content

Instantly share code, notes, and snippets.

@beatrizsmerino
Last active March 13, 2023 23:46
Show Gist options
  • Save beatrizsmerino/fd735aabc45fd93f7cfe57e9a8ecbfea to your computer and use it in GitHub Desktop.
Save beatrizsmerino/fd735aabc45fd93f7cfe57e9a8ecbfea to your computer and use it in GitHub Desktop.
Function 'svgMe' with jquery and vanilla javascript. This function converts an `<img>` tag, with a `.svg` extention and a class `svgMe`, into a `<svg>` tag. Example: https://codepen.io/beatrizsmerino/pen/pooMYdw
/**
* @function svgMe
* @description Version with Vanilla Javascript ES5
* Converts an `<img>` tag, with a `.svg` extention and a class `svgMe`, into a `<svg>` tag.
* @return {object} Return the file svg
*/
function svgMe() {
console.info(
"%cSvgMe__javascriptES5",
"padding: 0.2rem 0.5rem; background-color: rgb(17, 176, 200); color: #fff;"
);
var images = document.querySelectorAll("img.svgMe");
console.info("Array of images -> ", images);
images.forEach((image) => {
var imgId = image.getAttribute("id");
var imgClass = image.getAttribute("class");
var imgUrl = image.getAttribute("src");
var request = new XMLHttpRequest();
request.onreadystatechange = function () {
if (request.readyState == 4 && request.status == 200) {
console.info("request in xml -> ", request.responseXML);
callback(request.responseXML);
}
};
function callback(requestXML) {
var imgSvg = requestXML.querySelector("svg");
console.info("data type of 'data' -> ", typeof requestXML);
console.info("'data' -> ", requestXML);
console.info("images with svgMe -> ", imgSvg);
if (typeof imgId !== "undefined") {
console.info(imgId);
imgSvg.setAttribute("id", imgId);
}
if (typeof imgClass !== "undefined") {
console.info(imgClass);
imgSvg.setAttribute("class", imgClass);
imgSvg.classList.add("svgMe--replaced");
}
imgSvg.removeAttribute("xmlns:a");
if (
!imgSvg.getAttribute("viewBox") &&
imgSvg.getAttribute("height") &&
imgSvg.getAttribute("width")
) {
imgSvg.setAttribute(
"viewBox",
"0 0 " +
imgSvg.getAttribute("height") +
" " +
imgSvg.getAttribute("width")
);
}
image.replaceWith(imgSvg);
}
request.open("GET", imgUrl);
request.send();
});
}
/**
* @function svgMe
* @description Version with Vanilla Javascript ES6
* Converts an `<img>` tag, with a `.svg` extention and a class `svgMe`, into a `<svg>` tag.
* @return {object} Return the file svg
*/
function svgMe() {
console.info(
"%cSvgMe__javascriptES6",
"padding: 0.2rem 0.5rem; background-color: rgb(17, 176, 200); color: #fff;"
);
let images = document.querySelectorAll("img.svgMe");
console.info("Array of images -> ", images);
Array.from(images).map((image) => {
let imgId = image.getAttribute("id");
let imgClass = image.getAttribute("class");
let imgUrl = image.getAttribute("src");
const requestGetSVG = async (url) => {
const response = await fetch(url);
return response;
};
requestGetSVG(imgUrl)
.then((response) => {
console.info("Request with fetch -> ", response);
return response;
})
.then((response) => {
let string = response.text();
console.log("Data: ", string);
return string;
})
.then((string) => {
let data = new window.DOMParser().parseFromString(string, "text/xml");
console.log("Data: ", data);
callback(data);
})
.catch((error) => {
if (error.status === 404) {
console.log(
`%cError request: ${error}`,
"padding: 0.2rem 0.5rem; background-color: tomato; color: #fff;"
);
}
});
function callback(data) {
let imgSvg = data.querySelector("svg");
console.info("data type of 'data' -> ", typeof data);
console.info("'data' -> ", data);
console.info("images with svgMe -> ", imgSvg);
if (typeof imgId !== "undefined") {
console.info(imgId);
imgSvg.setAttribute("id", imgId);
}
if (typeof imgClass !== "undefined") {
console.info(imgClass);
imgSvg.setAttribute("class", imgClass);
}
imgSvg.removeAttribute("xmlns:a");
if (
!imgSvg.getAttribute("viewBox") &&
imgSvg.getAttribute("height") &&
imgSvg.getAttribute("width")
) {
imgSvg.setAttribute("viewBox", `0 0 ${imgSvg.getAttribute("height")} ${imgSvg.getAttribute("width")}`);
}
image.replaceWith(imgSvg);
}
});
}
/**
* @function svgMe
* @description Version with jquery
* Converts an `<img>` tag, with a `.svg` extention and a class `svgMe`, into a `<svg>` tag.
* @requires jQuery JavaScript library
* @return {object} Return the file svg
*/
function svgMe() {
$("img.svgMe").each(function() {
var $img = jQuery(this);
var imgID = $img.attr("id");
var imgClass = $img.attr("class");
var imgURL = $img.attr("src");
jQuery.get(
imgURL,
function(data) {
var $svg = $(data).find("svg");
console.info(data);
console.info($svg);
if (typeof imgID !== "undefined") {
$svg = $svg.attr("id", imgID);
}
if (typeof imgClass !== "undefined") {
$svg = $svg.attr("class", imgClass + " replaced-svg");
}
$svg = $svg.removeAttr("xmlns:a");
if (
!$svg.attr("viewBox") &&
$svg.attr("height") &&
$svg.attr("width")
) {
$svg.attr(
"viewBox",
"0 0 " + $svg.attr("height") + " " + $svg.attr("width")
);
}
$img.replaceWith($svg);
},
"xml"
);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment