I was right, it complicates the script (also because I added error checking and code to make it play well with other potential scripts on the page) but, simplifies the markup considerably. Put this script in the head:
Code:
<script type="text/javascript">
<!--
//Check Image Load © John Davenport Scheuer
//this credit must remain for legal use
function checkLoad(img){
var pics=document.images
for (var i_tem = 0; i_tem < pics.length; i_tem++)
if (pics[i_tem]==img)
loads[i_tem]=img.src
}
var loads=new Array()
function isLoaded(){
var pics=document.images
for (var i_tem = 0; i_tem < pics.length; i_tem++)
if (loads[i_tem]==undefined&&pics[i_tem].lowsrc)
pics[i_tem].src=pics[i_tem].lowsrc
}
if ( typeof window.addEventListener != "undefined" )
window.addEventListener( "load", isLoaded, false );
else if ( typeof window.attachEvent != "undefined" ) {
window.attachEvent( "onload", isLoaded );
}
else {
if ( window.onload != null ) {
var oldOnload = window.onload;
window.onload = function ( e ) {
oldOnload( e );
isLoaded();
};
}
else
window.onload = isLoaded;
}
//-->
</script>
Now the markup can look like this:
HTML Code:
<img src="no.jpg" onload="checkLoad(this)" />
<img src="no.jpg" lowsrc="1st.jpg" onload="checkLoad(this)" />
<img src="fold.gif" lowsrc="1st.jpg" onload="checkLoad(this)" />
Notice the onload event added to the first image with no lowsrc attribute. This is wrong but, will be ignored due to error checking. I could have added the image tags' onload checks dynamically but, Opera didn't like that, FF and IE thought it was fine. One other note, the lowsrc attribute is the alternate image and can be a different one for each image tag. If both the lowsrc and src are missing, you will get the regular broken image except in FF which, for some reason, renders no image at all. Also FF will hang trying to load the page at times if any of the primary images are not found but, with a refresh or two will load the alternate(s), if available, and the page in general if the alternate(s) is/are unavailable.
Bookmarks