I have a HTML page, and I want the text only (all text nodes).
Example HTML
<span>hello <strong>sir</strong></span>
Desired Output
hello sir
I have a HTML page, and I want the text only (all text nodes).
<span>hello <strong>sir</strong></span>
hello sir
Assuming you only want children of body
element...
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title> Example</title>
</head>
<body>
a <div>b<span>c</span></div>
</body></html>
var body = document.body;
var textContent = body.textContent || body.innerText;
console.log(textContent); // a bc
You need to check for textContent
because our good friend IE uses innerText
instead.
It is much easier if you have a library such as jQuery, i.e. $('body').text()
.
Also, it can be achieved on the server side, such as strip_tags()
in PHP. However, if you only wanted the body
element, you'd need to drill down to it using a DOM parser such as DOMDocument.
Assuming you are trying to get the html for the page your JS is residing on
var elems = document.getElementsByTagName('*');
var result = '';
for(var k in elems)
result += elems[k].innerHTML || '';
alert(result);
I am not sure I completely understand but if you want the markup for the current page then I guess you could make an Ajax request against the current page and use that:
$.get("/current-page-name", function(data) {
console.log(data);
});
http://jsfiddle.net/magicaj/CAWkx/