0
var str = document.getElementsByTagName('body');
str.toString();
var substring = "raju";
if(str.includes(substring)){
alert('found');
}

i want to parse the html as string for a particular website and checking for substring in that html code. But it not working, please help!

4 Answers4

0

You should add [0] to get first element from DOM collection and then access innerText property. toString is useless, hence removed

var str = document.getElementsByTagName('body')[0].innerText;
var substring = "raju";
if(str.includes(substring)){
alert('found');
}
Nurbol Alpysbayev
  • 19,522
  • 3
  • 54
  • 89
0

You should use document.body.innerHTML to get the HTML contents in a string. Example:

const str = document.body.innerHTML;
const substring = "raju";

if (str.includes(substring)){
 alert('found');
}
Karolis Ramanauskas
  • 1,045
  • 1
  • 9
  • 14
0

Includes is not supported on Internet Explorer. According to the MDN, so best way to do that is use indexOf

var str = document.body.innerHTML;
var substring = "raju";
if(str.indexOf(substring) >= 0) {
//your stuff
}
Negi Rox
  • 3,828
  • 1
  • 11
  • 18
0

This works and other suggested functions shouldn't be used. Ref: How to get the entire document HTML as a string?

    const str =new XMLSerializer().serializeToString(document)
    const substring = "raju";
    
    if (str.includes(substring)){
     alert('found');
    }
Jaro
  • 3
  • 3