Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Link object referencing associated Image object

    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?

  • #2
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    It's kludgy but it works.

    Code:
    <!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
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Posts
    400
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •