[Javascript] Problem changing link's href property...

Ben Curtis Quixote at LaMancha.org
Fri Jul 13 11:49:05 CDT 2001


> Still saying it's null or not an object.


1) both the links and anchors array are indexed-only and not associative
(key-value) arrays
2) that doesn't mean you can't change that is you want
3) you won't need to reference by name if you can get an event to pass the
object.

I started on this thread late, so tell me if I'm missing something. How are
you triggering this function? If you are doing it on (say) mouseover of the
link in question, then just pass the object as a parameter:

function imageOn(i, Lnk)
    { 
    document.images["Unit"].src = UnitImages[i].src;
    document.images["UnitNumber"].src = UnitNumberImages[i].src;
    Lnk.href = Page[i];
    }

<a href="LinkToPage.asp" onmousover="imageOn(1, this);">Link</a>

the "this" refers to the link in question, and you can therefore adjust its
href property in the function. If, on the other hand, you *really* need the
link to have a name referenced through the links array -- just make it:

<a href="LinkToPage.asp"
onmousover="document.links['JumpTo']=this;">Link</a>

The problem here is that you need the event to trigger this. How about
creating the associative array yourself, with something like this:

<a href="#JumpTo">Link named JumpTo</a>
<a href="#Foo">Link named Foo</a>
<a href="#Bar">Link named Bar</a>

<script>
for (Lnk in document.links) {
  if (Lnk.href.charAt(0) == "#")
    document.links[Lnk.href.substring(1)] = Lnk;
}
</script>

This will loop through all the links on the page, find those with "href"
values that start with "#", and then use the value after the # as the name
in the associative array. Since your function will reset the value of the
href before it gets used anyway, this is okay to put any old href you want
in there first. Note: the script needs to be *after* all the links on the
page, preferably just before the closing body tag.

--
+Ben Curtis
...keeping carpal tunnel at arm's length.










More information about the Javascript mailing list