...

View Full Version : Link object referencing associated Image object



andyf
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?

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



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

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


m_n

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

document.links[i].getElementsByTagName('img').length

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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum