JavaScript console.log outputting wrong array length -


first off want quite new javascript, not entirely new programming. familiar python , finished last semester @ college taking c++ , got a. semester taking javascript. feel bit silly asking question because seems quite stupid me, please nice me... anyway, first part of assignment write function count hyperlinks on webpage teacher provides. there should 20. so, teacher provides 2 lines of code create array of links , console.log them. copied , pasted them teacher has them on assignment page. 2 lines are:

var mylinks = document.getelementsbytagname("a"); console.log("links", mylinks.length); 

firstly, typed these 2 lines directly console on chrome provided website open, output 20. correct! happily copied , pasted 2 lines linked javascript file , reload page , produces 0. don't understand why... hope not silly question, coming c++ last semester, seems silly problem me... thank help!

update: whole javascript file. , 3 weeks semester , still feel bit on head... anyway, yes script tag needed @ end of body tag guess. did not realize mattered... apologize silly question... thank all, appreciate help.

<script> function getanchorlinkcount() {     var mylinks = document.getelementsbytagname("a");     console.log("links", mylinks.length); } </script> 

then

<body onload="getanchorlinkcount()"> 

you suppose count when dom entirely loaded


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -