0

I'm trying to get the entire HTML of a page, but it seems that the text stops after </head>. The following code is essentially how I tested this. What am I doing incorrectly here?

<html>
<head>
    <script>
        document.onload = showHTML();
        function showHTML() {
            html = document.documentElement.innerHTML;
            alert(html);
        }
        </script>
</head>
<body>
    <p> This is absolutely useless text. </p>
</body>
</html>
fakedad
  • 1,292
  • 1
  • 10
  • 21

4 Answers4

2

Okay here is a complete working answer... after checking already posted answer I realized it didn't work for multiple reasons..

First you need to put a function in the onload event. The onload event is written without uppercases.

Also! you need to put the event on the window object as such:

window.onload = showHTML;

Here is a fiddle. Notice on the left that it isn't wrapped inside onload. It's unwrapped in head like your code should be.

http://jsfiddle.net/4zsGH/2/

You should have something like this:

<html>
<head>
    <script>
        window.onload = showHTML;
        function showHTML() {
            var html = document.documentElement.outerHTML;
            alert(html);
        }
        </script>
</head>
<body>
    <p> This is absolutely useless text. </p>
</body>
</html>
Loïc Faure-Lacroix
  • 13,220
  • 6
  • 67
  • 99
1

Take off the parenthesis from document.onLoad = showHTML();

What's happening is showHTML() is being called right away, before the rest of the document is being loaded. Taking off the parenthesis means the function is being set to the onLoad callback.

at.
  • 50,922
  • 104
  • 292
  • 461
0

Try:

<html>
<head>
    <script>
        document.onload = showHTML;
        function showHTML() {
            var html = document.documentElement.outerHTML;
            alert(html);
        }
        </script>
</head>
<body>
    <p> This is absolutely useless text. </p>
</body>
</html>

When you wrote document.onLoad = showHTML(); you didn't assign the reference to showHTML function to document.onLoad but you assigned the value returned by that function i.e. undefined (because you called it). I also changed innerHTML to outerHTML.

Also document.onload shouldn't be written in camel case.

Writing var html = … isn't essential but it wouldn't run in strict mode. Without it you create a html property on global object window implicitly.

kamilkp
  • 9,690
  • 6
  • 37
  • 56
  • Add a description of what you did to fix the problem. – Loïc Faure-Lacroix Feb 24 '14 at 08:02
  • Removing the parentheses from the function seems to make the function not run at all. Changing to var seems to do nothing. Changing to outerHTML gets me the tags, but it still does not include the body. I've also tried all combinations of each of these fixes, and none seem to work. – fakedad Feb 24 '14 at 08:10
  • make sure you write `document.onload`, not `document.onLoad` – kamilkp Feb 24 '14 at 08:12
-2

I think this is what you are looking for:

    document.onLoad = showHTML();
    function showHTML() {
       var html = document.documentElement.innerHTML;
        alert(html);
    }

http://jsfiddle.net/skhan/4zsGH/

Dot_NET Pro
  • 2,095
  • 2
  • 21
  • 38
  • What your are missing is declare html var. – Dot_NET Pro Feb 24 '14 at 07:58
  • It doesn't matter, the fact is that the showHTML is called before the onload event is triggered. You're putting undefined inside onLoad. The fiddle works because the code is wrapped in a onLoad. – Loïc Faure-Lacroix Feb 24 '14 at 08:00
  • In your fiddle you are executing this script already after the DOM is rendered, and making the same mistake as the OP: assiging the result of showHTML function to document.onload, not the reference to that function. – kamilkp Feb 24 '14 at 08:00
  • Declaring the var does not seem to solve the problem. – fakedad Feb 24 '14 at 08:08