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

Anthony E. apwebdesign at yahoo.com
Fri Jul 13 17:53:07 CDT 2001


similar to mine, only I built an array of all the
links  on the page containing each href.

then called onMouseOver="swap(this.href,
'newhref.html')">link N</a>

swap() function compares 'this.href' to each href in
the  'docLinks' array, then you know what index the
link is on the page...rather than having to know ahead
of time which docLink[i] you're calling from, as the
order of links on the page will undoubtedly change
over time.

when the loop finds "docLinks[i] == swapHref" it
changes the value of that link in the DOM to
'newHref'.

here's a copy in case my earlier attachment didn't
come thru.

originally, refering to the link by 'name' in the DOM
worked for IE4 (document.all.name.href), but not
NN4...thus the result is the following link indexing
scheme, which works in both browsers...

(I could probably modify this a bit to build a
multi-dimensional array that contained a 'new' & 'old'
href value for each link on the page, but the
following suffices for the original inquiry)

=======

<html>
<head>
<script language="JavaScript" type="text/javascript">
<!-- // swap href in links
//accepts two parameters: href of link calling swap(),
and the new url to swap with.

function swap(swapHref, newHref) {
var docLinks = new Array();
  for (i=0; i < document.links.length; i++) {
   docLinks[i] = document.links[i].href;
//   alert(docLinks[i]);
  }
  for (i=0; i<docLinks.length; i++) {
    if (docLinks[i] == swapHref) {
      document.links[i].href = newHref;
//      alert(newHref);
    }
  }
}
// -->
</script>
</head>

<body>

<a href="test.html" onMouseOver="swap(this.href,
'new.html');">test</a><br><br>
<a href="test2.html" onMouseOver="swap(this.href,
'new2.html');">test2</a>
</body>
</html>


===============














--- Ben Curtis <Quixote at LaMancha.org> wrote:
> 
> > 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.
> 
> 
> 
> 
> 
> 
> 
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> http://www.LaTech.edu/mailman/listinfo/javascript


__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail
http://personal.mail.yahoo.com/



More information about the Javascript mailing list