-3

In both of the following examples, speechTextString ends up including text from the entire document, including text outside of the BODY tag.

Example 1:

r = document.body;
speechTextString = r.innerText;

Example 2:

r = document.getElementsByTagName("BODY").item(0);
speechTextString = r.innerText;

Is it possible to get the root HTML tag, not just the body element?

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
JefferyD
  • 23
  • 3

1 Answers1

1

text outside of the BODY tag

Assuming you aren't talking about the content of the <head> element:

You can't have text outside the <body> element. HTML does not allow it. The browser performs error recovery and moves it inside the <body> element when it parsing the (invalid) HTML into a DOM.

If you want to process a document with that error in it, then you will need to fetch the raw source code (e.g. with XMLHttpRequest) and then write a custom parser for it.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335