0

I'm beginner in Javascript.

I'm learning Browser Object Model.

Then, I had a error message from Chrome console.

My codes are below.

<html>
<head>
    <title>
    </title>
    <meta charset="UTF-8" />
    <script type="text/javascript">
    function showLinks() {
        showLinks_NOT_ERROR1();
        showLinks_NOT_ERROR1();
        showLinks_WANTED_BUT_ERROR();
    }

    function showLinks_NOT_ERROR1() {
        console.log(document.links[0].href);
        console.log(document.links[1].href);
    }
    function showLinks_NOT_ERROR2() {
        alert(document.links[0].href);
        alert(document.links[1].href);
    }
    function showLinks_NOT_ERROR3() {
        document.write(document.links[0].href);
    }
    function showLinks_WANTED_BUT_ERROR() {
        document.write(document.links[0].href);
        document.write(document.links[1].href);
    }

    </script>
</head>
<body onload="showLinks();">
    <a href="http://www.google.com">google</a>
    <a href="http://www.yahoo.com">yahoo</a>
</body>
</html>

As you could see in showLinks_NOT_ERROR3, just one document.write(document.links[0].href); has no error but two document.write(document.links[0].href); document.write(document.links[1].href); has error.

Why does this error happen?

SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
ChoYongha
  • 1
  • 3

1 Answers1

0

Further to my comment: When using document.write always note:

'document.write()' is used to write to the document stream.

Calling 'document.write()' on a closed document stream automatically calls document.open(), which will clear the document.

See the correct answer marked in this post: document.write clears page

<!DOCTYPE html>
<html>
    <head><title>Test</title></head>
    <body>

    <a href="http://www.google.com">google</a>
    <a href="http://www.yahoo.com">yahoo</a>

        <script type="text/javascript">
        function showLinks() {
            showLinks_NOT_ERROR1();
            showLinks_NOT_ERROR2();
            showLinks_WANTED_BUT_ERROR();
        }

        function showLinks_NOT_ERROR1() {
            console.log(document.links[0].href);
            console.log(document.links[1].href);
        }
        function showLinks_NOT_ERROR2() {
            alert(document.links[0].href);
            alert(document.links[1].href);
        }
        function showLinks_NOT_ERROR3() {
            document.write(document.links[0].href);
        }
        function showLinks_WANTED_BUT_ERROR() {
            document.write(document.links[0].href);
            document.write(document.links[1].href);
        }
        showLinks();
        </script>
    </body>
</html>

This code should work for you. Here the showLinks function is called before the document stream is closed.

Community
  • 1
  • 1
Dev Shangari
  • 336
  • 2
  • 7
  • Thank you. I read your comment and related posts. So I thought that your code works because it called document.write before my first document is closed. But I copied and pasted it, it still happened same error. – ChoYongha Sep 24 '15 at 00:12
  • @ChoYongha updated my code - removed the onload function call on the body - i just copied it over from your html example above – Dev Shangari Sep 24 '15 at 09:22