Currently, I am facing an issue with parsing an SVG after loading it using XMLHttpRequest. The code snippet for loading the SVG is as follows:
var svgDoc;
var xhr = new XMLHttpRequest();
xhr.open("GET", "data/vectors.svg", true);
xhr.onload = function (e) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
svgDoc = xhr.responseText;
console.log(svgDoc.getElementsByTagName("svg"));
} else {
console.error(xhr.statusText);
}
}
};
xhr.onerror = function (e) {
console.error(xhr.statusText);
};
xhr.send(null);
An error of 'Uncaught TypeError: undefined is not a function' occurs when trying to access the `getElementsByTagName` method. Even logging `console.log(svgDoc.getElementsByTagName)` returns 'undefined'. It's perplexing since SVG is essentially XML, and yet any XML DOM methods cannot be invoked on the SVG object. What could be causing this behavior?