How to find out the anchor link href value of a given image?

  • Follow


Hello:

My html file contains the following image link:

<a href="some_url"><img src="MyImage.gif"></a>

How can I use Javascript to find out the value of some_url for the
given image name "MyImage.gif"?

I know how to get the image object with a given name (e.g.
"MyImage.gif"). But then how do I get the parent anchor link object and
find its href value?

0
Reply ningjun.wang (11) 8/5/2005 3:09:46 PM

ningjun.wang@lexisnexis.com wrote:
> Hello:
> 
> My html file contains the following image link:
> 
> <a href="some_url"><img src="MyImage.gif"></a>
> 
> How can I use Javascript to find out the value of some_url for the
> given image name "MyImage.gif"?
> 
> I know how to get the image object with a given name (e.g.
> "MyImage.gif"). But then how do I get the parent anchor link object and
> find its href value?
> 
this may or may not work: if the image has an id use this:
var it=document.getElementById('imageid').parentNode.href;
that said i may have gotten it wrong completely :p

-- 
Hope This Helped and MTFBWY...
Kieren aka JediFans - <URL:http://jedifans.com/>
The Force Is With Me, SuSE Linux Professional 9.3, Mozilla Firefox 
1.0.6, Mozilla Thunderbird 1.5 Alpha 2 and Revenge Of The Sith!
0
Reply Jedi 8/5/2005 3:37:53 PM


ningjun.wang@lexisnexis.com wrote:
> Hello:
> 
> My html file contains the following image link:
> 
> <a href="some_url"><img src="MyImage.gif"></a>
> 
> How can I use Javascript to find out the value of some_url for the
> given image name "MyImage.gif"?
> 
> I know how to get the image object with a given name (e.g.
> "MyImage.gif"). But then how do I get the parent anchor link object and
> find its href value?
> 

document.images['myImage_1'].parentNode.href

file test.htm :
<html>
<script type="text/javascript"><!--
function linkOfImage(pict) {
var Href=null, Link, thisImg;
var I = document.getElementsByTagName('IMG');
for(var i=0;i<I.length;i++) {
  thisImg = I[i].src.substring(I[i].src.lastIndexOf('/')+1)
  if(pict==thisImg) {
    Link = I[i].parentNode;
    if(Link && Link.tagName.toLowerCase()=='a')
    Href = Link.href;
    }
  }
if(Href) alert('link href = '+Href);
else alert('not found');
}
// --></script>
<p><a href="#" onclick="linkOfImage('i_2.jpg');">image href i_2.jpg</a>
<p><a href="t_1.htm"><img src="i_1.jpg"></a>
<p><a href="t_2.htm"><img src="i_2.jpg"></a>
<p><a href="t_3.htm"><img src="i_3.jpg"></a>
</html>


-- 
Stephane Moriaux et son [moins] vieux Mac
0
Reply ASM 8/5/2005 4:18:12 PM

<ningjun.wang@lexisnexis.com> wrote in message 
news:1123254586.639821.158520@z14g2000cwz.googlegroups.com...
> Hello:
>
> My html file contains the following image link:
>
> <a href="some_url"><img src="MyImage.gif"></a>
>
> How can I use Javascript to find out the value of some_url for the
> given image name "MyImage.gif"?
>
> I know how to get the image object with a given name (e.g.
> "MyImage.gif"). But then how do I get the parent anchor link object 
> and
> find its href value?

<a href="http://www.yahoo.com"><img name="test" src="test.gif"></a>
<button onclick="alert(getParentHrefFromImage('test'));">Find 
href</button>
<script type="text/javascript">
function getParentHrefFromImage(imageName)
{
  if (document.images && document.images[imageName])
  {
    var href;
    for (var el = document.images[imageName]; (el = el.parentNode);)
    {
      if ('string' == typeof el.tagName &&
          'a' == el.tagName.toLowerCase())
      {
          return el.href;
      }
    }
  }
  return null;
}
</script>

-- 
Grant Wagner <gwagner@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
 


0
Reply Grant 8/5/2005 5:31:07 PM

3 Replies
95 Views

(page loaded in 0.189 seconds)


Reply: