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?