On Jun 2, 10:03 am, BinnyVA <binn...@gmail.comwrote:
HiI need to get all links from an document element. In sample below how
can i get link to "www.google.com" from element with ID="here"
RobG's solution should work fine. However, if you have a big list of
urls on the page and you want them all, you need to do this...
var links = document.getElementsByTagName("a");
for(var i = 0; i<links.length; i++) {
//Do whatever you want with the links
alert(links[i].href)
}
A caveat is that it you will get all anchors as well ( like <a
name="section3")
For each document on the parsing stage two collections are being
automatically created:
document.links - with all links so all A with HREF attribute set
document.anchors - all A with NAME attribute set
IMO on big documents, rather than using a rather resource consuming
getElementsByTagName it is more straightforward to use these pre-
sorted collection:
var lnk = document.links;
var len = lnk.length;
for (var i=0; i<len; i++) {
// do what you need with
// lnk[i]
}