Get links from an document element

  • Thread starter Miroslav Stampar [MCSD.NET / Security+]
  • Start date
M

Miroslav Stampar [MCSD.NET / Security+]

I 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"


<html>

<head>

<title></title>

<script>
function bla()
{
window.alert("HELLO");
}
</script>

</head>

<body>
<td id="here">
<a href="http://www.google.com">Google</a>
</td>

<script>
document.getElementById("here") ........................WHAT?
</script>

</body>

</html>


Please help.
 
R

RobG

I 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" [...]
<body>
<td id="here">
<a href="http://www.google.com">Google</a>
</td>

<script>
document.getElementById("here") ........................WHAT?
</script>

Assuming that you have valid HTML, the trivial solution is:

var hereTd = document.getElementById('here');
var hereHref = hereTd.getElementsByTagName('a')[0].href;
 
B

BinnyVA

Hi
I 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.href)
}

http://developer.mozilla.org/en/docs/DOM:document.getElementsByTagName
- getElementsByTagName() Reference
 
V

VK

Hi> I 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.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
}
 
M

Miroslav Stampar [MCSD.NET / Security+]

RobG, you are the man :). I owe you.

Thank you all for your time spent.

RobG je napisao/la:
I 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" [...]
<body>
<td id="here">
<a href="http://www.google.com">Google</a>
</td>

<script>
document.getElementById("here") ........................WHAT?
</script>

Assuming that you have valid HTML, the trivial solution is:

var hereTd = document.getElementById('here');
var hereHref = hereTd.getElementsByTagName('a')[0].href;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top