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>
2
  • declare var html = document.documentElement.innerHTML; Commented Feb 24, 2014 at 8:00
  • @Dot_NETJunior it's not the problem here. html will be created on the global object. Commented Feb 24, 2014 at 8:01

4 Answers 4

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>
Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

This does not seem to solve the problem. When I try this solution, it seems that the showHTML() function does not run at all.
@fakedad try document.onload no uppercase letter.
Changing the case doesn't seem to fix it when the parentheses are absent.
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.

3 Comments

Add a description of what you did to fix the problem.
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 <html> 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.
make sure you write document.onload, not document.onLoad
-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/

4 Comments

What your are missing is declare html var.
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.
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.
Declaring the var does not seem to solve the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.