0

Example:

  1. Fetch call that gets a HTML document as response.
  2. Response gets interpreted as DOM structure.
  3. One can now use querySelector to search for specific elements in that structure.

My idea so far (probably way more complex than it should be):

const responseString = await(await fetch('https://.....')).text();
const element = document.createElement('html');
element.innerHTML = responseString;

There has to be a standard for this right?

Something like: const responseString = await(await fetch('https://.....')).domNodes();

codepleb
  • 10,086
  • 14
  • 69
  • 111
  • I don't see a problem with your current approach. HTML string to DOM is a pretty standard `.innerHTML` use case. – ggorlen Jun 07 '22 at 15:40
  • 2
    @ggorlen - Well, in theory the code above tries to create an invalid HTML structure, because the text presumably has `` at the beginning if not ` `, neither of which is valid as a child of an `html` element. [`DOMParser`](https://developer.mozilla.org/en-US/docs/Web/API/DOMParser) is the cleaner way to do it. But yeah, it would work. :-) – T.J. Crowder Jun 07 '22 at 15:42
  • I see, good point. Can they use `.outerHTML` for the same effect? – ggorlen Jun 07 '22 at 15:42

0 Answers0