Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

image swap IE6

Status
Not open for further replies.

wrmedia

Technical User
Jun 6, 2002
19
US
having problems with my code specifically in IE6

code =

<script language=&quot;JavaScript&quot;><!--

function flip(name,src) { document.name.src = src; } //--></script>

refers to this as an example =

<a href=&quot;services.html&quot; onClick=&quot;flip('general1','servicesin.gif');return false&quot;><img SRC=&quot;servicesout.gif&quot; name=&quot;general1&quot; width=135 height=29 border=0></a>

any suggestions? thanks
 
sorrentox,
try this:

<html>
<head>
<script language=&quot;javascript&quot;>
// preload image to be substituted
var offImage=new Image();
offImage.src=&quot;servicesin.gif&quot;;

//refer to image like this in the function
function flip(name,src){
document.getElementById(name).src=src;
}
</script>
</head>
<body>
<!-- use &quot;id&quot; instead of (or as well as) &quot;name&quot;-->
<a href=&quot;#&quot; onClick=&quot;flip('general1','servicesin.gif');return false&quot;><img src=&quot;servicesout.gif&quot; id=&quot;general1&quot; name=&quot;general1&quot; width=135 height=29 border=0></a>
</body>
</html>

sundemon
 
sundemon,
Using getElementById is very bad idea because it cuts off the 4th generation browsers. They don't support getElementById but they are definitely capable of showing rollovers!

sorrentox,
The following code works in all browsers where images[] array exist (starting from Netscape 3 and IE4):
[tt]
function flip(imgNumb,imgFile) {
if (document.images)
document.images
.src = eval(imgFile + '.src');
}
[/tt]
Call it the same way as you did in your code.
 
sorrentox and starway,
Sorry sorrentox, starway's code is certainly simpler, better and more universal. However, I get an error from the &quot;eval(imgFile+'.src)&quot; part. Instead I find it works if I just use the image file name string, &quot;imgFile&quot; in the right hand side of the function.

Thanks for pointing out the better way, though.
sundemon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top