I am facing an issue with the following HTML code:
<html>
<script>
function explode() {
pp = document.getElementsByTagName("a");
for(i=0;i<pp.length;i++) {
document.write(pp[i].href);
}
}
</script>
<body>
<a href="http://google.com">Google</a>
<a href="http://yahoo.com">Yahoo</a>
<button onclick="explode()">Explode</button>
<body>
</html>
After executing this code, only the first hyperlink is being printed on my window. Can anyone provide an explanation for this behavior?
Update
I have considered the response that document.write
resets the page contents. However, I expected to see the last element in the variable pp
since all objects are listed within it. Why does it display the first element instead?