View Full Version : Link object referencing associated Image object

07-06-2005, 05:09 PM
In accessing links from the document.links[] collection, which yields Link objects, I am attempting to determine whether the link is a text based link or an image link. The first case is fairly easy as the text property of the Link object won't be empty. However, the second case of being an image link comes with the issue of relating to the image.

Is there a native method to correlate a Link object with the Image object?

07-06-2005, 10:31 PM
It's kludgy but it works.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
function linkTypes() {
for( var i=0; i<document.links.length; i++ ) {
alert( (document.links[i].firstChild.src) ? "image" : "string" );

<body onload="linkTypes();">
<a href="http://www.codingforums.com"><img src="logo.gif" border="0"></a>
<a href="http://www.codingforums.com/showthread.php?t=62886">This is the second link</a>


Harry Armadillo
07-07-2005, 05:00 AM
I'd be more comfortable with


as the test for image children. If you're into pretty, indented source then an image link could easily have a white-space node as the link's first child.

Ideally you should look at/for both images and text, so as to spot the popular "icon and text" style of links.