rollover code acting up
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>
Troy1960 posted this at 15:03 — 27th October 2004.
They have: 152 posts
Joined: Sep 2004
http://www.scit.wlv.ac.uk/~jphb/cp3024/javascript/examples/ex1d.html
Greg K posted this at 21:53 — 27th October 2004.
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.