I have been trying to parse html string to DOM nodes and i keep having the same issue with the anchor links irrespective of the approach i use,
Here is the html string for testing:
let htmlString = '<div><h2>Absolute URLs</h2><p><a href="https://www.w3.org/">W3C</a></p><p><a href="https://www.google.com/">Google</a></p><h2>Relative URLs</h2><p><a href="html_images.asp">HTML Images</a></p><p><a href="/css/default.asp">CSS Tutorial</a></p></div>';
and i try to parse by the following methods and i get the same output, not sure how to resolve this,
let template = document.createElement('template');
htmlString = htmlString.trim();
template.innerHTML = htmlString;
console.log(template.content.firstChild);
let docs = new DOMParser().parseFromString(htmlString,'text/html');
console.log(docs.body.firstChild);
The converted DOM is following where you can see that the slashes in the anchor tags are getting replaced with empty character and href is repeated in the closing anchor tag, its with all the anchor tags
<div><h2>Absolute URLs</h2><p><a href="https: www.w3.org="" "="">W3C</a href="https:></p><p><a href="https: www.google.com="" "="">Google</a href="https:></p><h2>Relative URLs</h2><p><a href="html_images.asp">HTML Images</a href="html_images.asp"></p><p><a href=" css="" default.asp"="">CSS Tutorial</a href="></p></div>
any help to how to go about it would be highly appreciated.
Via Active questions tagged javascript - Stack Overflow https://ift.tt/a1MZYkI
Comments
Post a Comment