rollover code acting up

They have: 4 posts

Joined: Oct 2004

the rollover code im using is acting up, heres the prob: if i move the mouse over and image that has a rollover while the page is loading it makes the img dissapear and leaves an empty box with the red x on it...what ami i doing wrong?

any and all help is greatly appreciated, thanks.

code:

<script type="text/javascript">

function init() {
if (!document.getElementById) return
var imgOriginSrc;
var imgTemp = new Array();
var imgarr = document.getElementsByTagName('img');
for (var i = 0; i < imgarr.length; i++) {
if (imgarr[i].getAttribute('hsrc')) {
imgTemp[i] = new Image();
imgTemp[i].src = imgarr[i].getAttribute('hsrc');
imgarr[i].onmouseover = function() {
imgOriginSrc = this.getAttribute('src');
this.setAttribute('src',this.getAttribute('hsrc'))
}
imgarr[i].onmouseout = function() {
this.setAttribute('src',imgOriginSrc)
}
}
}
}
onload=init;
</script>

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

If you wait until the page is completely loaded, do the rollovers work right?

If so, and this is purely guessing here so you may have to play around, then the images may not be loaded before you initial the swap. You have at the end of your script "onload=init" which makes it look like you are intending the init function to only execute once the page is loaded, however I think to do this right, you need to do . I could be wrong, but give this a try instead.

-Greg

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.