Click to See Complete Forum and Search --> : help with dynamic href values


iamfm
01-15-2004, 03:35 PM
Hi all,
I'm attempting to create a script that can not only swap images but update the href value as well. I'm quite new to this so my scripting abilities are horrible. Below is a simple version of what I am trying to accomplish. So far this script works in IE6,NS7 for PC but no versions lower than this. Nor does it work in any Mac-based browser version. If anyone knows a better way to accomplish my goal, I would really appreciate any input.
Thanks!


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script language="JavaScript" type="text/JavaScript">
function newLocation() {
document.location.href="http://www." + adURL
}
</script>

<title>dynamic test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<table width="460" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="300">&nbsp;</td>
<td width="160"><a href="javascript:newLocation()"><img src="images/apples.jpg" name="fruit" width="160" height="160" border="0" id="fruit"></a></td>
</tr>
<tr>
<td width="300">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td width="300">&nbsp;</td>
<td align="center" valign="bottom"><a href=# onClick="document.fruit.src='images/oranges.jpg';adURL='yahoo.com';">Swap</a></td>
</tr>
</table>
</body>
</html>

Jona
01-15-2004, 11:48 PM
Send the value to the function:


function newLocation(adURL){
window.location.href = "http://www."+adURL;
}


And call the function like so:


onclick="newLocation('yahoo.com');"


[J]ona

iamfm
01-16-2004, 10:16 AM
thanks Jona !
That script does work. But what I am tyrying to do is pass the value of adURL from a link somewhere else on the page.
Hopefully, I can calrify.
1. So I have this script in the head:
<script language="JavaScript" type="text/JavaScript">
function newLocation() {
document.location.href="http://www." + adURL
}
</script>
2. And there's this image wrapped in an <a> tag
<a href="javascript:newLocation()"><img src="images/apples.jpg" name="fruit" width="160" height="160" border="0" id="fruit"></a>
3. the function of this next link is when the user clicks on it, the image above is not only swapped, but the value of the href is updated
<a href=# onClick="document.fruit.src='images/oranges.jpg';adURL='yahoo.com';">Swap</a>

This actually works in IE6x for PC and Netscape 6 +5 for PC. I've tried it on Ie5x and IE6x for PC and Mac and Safari and the "fruit" link does not work, nor do I get an error message, so I'm not sure what I'm doing wrong.

Again, thank you so much for any advice!!!!

Jona
01-16-2004, 04:34 PM
Actually, you don't even need a function...


<a href="http://www.yoursite.com/apple.html"><img src="images/apples.jpg" name="fruit"></a>

<a href="#" onclick="for(i=0; i<document.links.length; i++){if(document.links[i].href='http://www.yoursite.com/apple.html'){ document.links[i].href='http://www.yoursite.com/oranges.html';} document.images['fruit'].src='images/oranges.jpg';}">Switch</a>


[J]ona