/    Sign up×
Community /Pin to ProfileBookmark

Changing ‘HREF’ in an anchor tag

I’m running a simple javascript slideshow and I’ve set the default HREF at
<a href=”#” name=”slideshow_href”>
<img src=”any_slideshow_image” name=”slideshow”>
</a>.

Is there any way to dynamically change that href using DHTML?

In particular I’d like to have the image display in a new window whenever a visitor clicks on it.

TIA
Terry

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisMay 09.2004 — You can use the onclick event of the link to do just about whatever you want, including change the href.
<i>
</i>&lt;a href="#" ... onclick="this.href='somelink.htm'"&gt;

or
<i>
</i>&lt;a href="#" ... onclick="window.location.href='somelink.htm';return false"&gt;

You can even call a function if you need to get complex:
<i>
</i>&lt;a href="#" ... onclick="doIt();return false"&gt;
Copy linkTweet thisAlerts:
@David_HarrisonMay 09.2004 — Here's a script without a link, there's no need for a link because you can just use onclick for the image.

[upl-file uuid=d028ca02-3e2e-4b91-b34c-cab9109ce66e size=1kB]cycle through images.txt[/upl-file]
Copy linkTweet thisAlerts:
@cupojava1authorMay 09.2004 — Thanks for the replies but I figured it out with a very simple solution. The image URLs are stored in an array named Pic. The Image tag has the NAME attribute of 'SlideShow'

<img src="first image url" name="SlideShow">

Since I already have the image URLs in an array I just replace the HREF using the following code.

// Replace HREF

var x=document.anchors;

x[1].href = Pic[j]; //this replaces the second anchor on the page


var j=0

function runSlideShow()

{

// Replace HREF

var x=document.anchors;

x[1].href = Pic[j]; //this replaces the second anchor on the page

// Replace Image (images were loaded into the Array 'preLoad'

document.images.SlideShow.src = preLoad[j].src;

// Replace Image number and description text

s_show_text.value = "Image " + (j+1) + ": " + PicInfo[j];

j = j + 1;
if (j > (p-1)) j=0;
t = setTimeout('runSlideShow()', slideShowSpeed);

}


One thing I noticed was that this wouldn't work unless I actually give the Anchor a NAME attribute even though it isn't referenced by name. (Can it be referenced by name?)

<a href="first image url" name="SlideShowHREF">

<img src="first image url" name="SlideShow">

</a>

Anyway, thanks for your responses. If you care to see the slide show the subject is "Ancient and Medieval UFO Art" there are only 21 images but more will be added soon. For those on dialup it may take a while for all the images to load.

http://mufondfw.com/

Click on 'Slideshow'

Thanks All

Terry Groff

Webmaster: The Dallas-Fort Worth Mutual UFO Network (DFW MUFON)
Copy linkTweet thisAlerts:
@gil_davisMay 09.2004 — [b](Can it be referenced by name?)[/b][/quote]Yes, it can. Use the name instead of the number 1. Make sure you enclose the name with quotes because it is a string.
Copy linkTweet thisAlerts:
@cupojava1authorMay 09.2004 — [i]Originally posted by gil davis [/i]

[B]Yes, it can. Use the name instead of the number 1. Make sure you enclose the name with quotes because it is a string. [/B][/QUOTE]


// Replace HREF

var x=document.anchors;

x["SlideShowHREF"].href = Pic[j];

I tried this but I couldn't get it to work.

What is the syntax for referencing NAME in an <a> tag?

I have no problem referencing NAME in Form Fields (<input>, <textarea>, etc) but I have never been able to in an <a> tag.

Thanks

Terry
Copy linkTweet thisAlerts:
@gil_davisMay 09.2004 — I made some experiments, and it would seem that IE6 has a bit of a problem with that. However, if you usedocument.getElementsByName("whatever")[0]it seems to behave.

Netscape 7 works as expected using either method.
Copy linkTweet thisAlerts:
@cupojava1authorMay 09.2004 — [i]Originally posted by gil davis [/i]

[B]I made some experiments, and it would seem that IE6 has a bit of a problem with that. However, if you usedocument.getElementsByName("whatever")[0]it seems to behave.

Netscape 7 works as expected using either method. [/B][/QUOTE]


Thanks Gil,

I now have two ways of referencing HREF

I used this code and it works fine in IE6 and Opera7

document.getElementsByName("SlideShowHref")[0].href = Pic[j];[/QUOTE]

Thanks again

Terry
×

Success!

Help @cupojava1 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.13,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...