473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Get links from an document element

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.

Jun 2 '07 #1
4 23804
On Jun 2, 10:08 am, "Miroslav Stampar [MCSD.NET / Security+]"
<miroslav.stam...@gmail.comwrote:
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;
--
Rob

Jun 2 '07 #2
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[i].href)
}

http://developer.mozilla.org/en/docs...mentsByTagName
- getElementsByTagName() Reference

--
Binny V A
http://www.openjs.com/ - JavaScript, Opened

Jun 2 '07 #3
VK
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]
}
Jun 2 '07 #4
RobG, you are the man :). I owe you.

Thank you all for your time spent.

RobG je napisao/la:
On Jun 2, 10:08 am, "Miroslav Stampar [MCSD.NET / Security+]"
<miroslav.stam...@gmail.comwrote:
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;
--
Rob
Jun 2 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tor Hovland | last post by:
I'm trying to transform the document element of incoming xml files, however, I'm having trouble with namespace references not appearing correctly. Here's an example input file: <?xml...
1
by: Andy Fish | last post by:
Hi, I'm using XML DOM in Java and I have a function like this Element makeSomeXml(Document doc) {..} This returns an Element owned by (but not appended to) the specified document. Now,...
9
by: s_m_b | last post by:
I'm trying to get an <a> element to gain the focus onload, but only get back 'has no properties'. Reading through this ng, its clear that unless the element is within a form, this doesn't happen,...
10
by: b.dam | last post by:
I'm trying the following: function grid() { this._el = document.createElement("TABLE"); var self = this; document.addEventListener("onkeydown", function(event) {self.gridKeyDown(event);},...
0
by: Dave19 | last post by:
Hi everybody, Well I have a svg file as shown below: <svg width='500' height='550' viewbox='40 40 500 550' xmlns='http://www.w3.org/2000/svg'> <image x='40' y='40' width='500' height='550'...
4
by: BizTalk Benjamin | last post by:
Hi, I have an XmlDocument loaded from a memory stream. I set the document element prefix in this way XmlElement e = xDoc.DocumentElement; e.Prefix = "abc" When i simply write the document...
17
by: pbd22 | last post by:
hi. i keep getting this error. as i understand it, my xml isn't formatted correctly. the online errata suggests the standard formatting to solve this problem: element (tab) (tab) element
2
by: pbd22 | last post by:
Hi. I have an event handler in VB.NET that gets called several times. The output of the event handler is XML that is being called by an xmlhttp request from the client. I am using a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.