13

I need to fetch body tag from the url which i open. I am using following code but it is not working. Please suggest.

document.getElementsByTagName('body')[0].innerHTML;

Thanks Tanu

eckes
  • 64,417
  • 29
  • 168
  • 201
Tanu Garg
  • 175
  • 1
  • 1
  • 3
  • 1
    Insufficient information in this question to know what might be going on. More of the relevant code and timing of calling this code is needed. – jfriend00 Jan 23 '12 at 13:47

4 Answers4

19

Copy below code in url navigation bar. See, whole body content alerted.

javascript:(alert(document.body.innerHTML));

Its means you can get the body content by just body.innerHTML. you don't require to use getElementsByTagName.

Umesh Patil
  • 10,475
  • 16
  • 52
  • 80
3

Did you try document.body.innerHTML?

Luminously
  • 299
  • 1
  • 6
2

Here is my simple test

<html>
<head>
<title>TEST </title>
<script type="text/javascript">
MyFunction = function () {
    alert(document.body.innerHTML);
};
</script>
</head>
<body onload="MyFunction();">

<h1>My First Heading</h1>

<p>My first paragraph.</p>

</body>
</html>

it's working on FF

Sujoy
  • 57
  • 7
0

Assuming that:

  • The script runs after the document has loaded
  • The script is running on the page that you want the body content from (which would not include the address bar in Firefox).

That that will work, although it is uglier than document.body.innerHTML.

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