I am posting this for those looking for a simple image swap routine that handles two images of different dimensions. Many routines (all simple) worked locally but not on the server I use. The following code worked across several browsers, locally and on my server.
Hopefully this may save someone a few minutes. I am not a programmer, just a technical user of JavaScript.
Always helpful to preload images...
Simple javascript code for adjusting varying dimensions..
..and finally the image..
Of course there are numerous approaches but after many trials I found this worked across all situations I encountered [did not test on Safari]
Hopefully this may save someone a few minutes. I am not a programmer, just a technical user of JavaScript.
Always helpful to preload images...
Code:
// This global variable contains an instance of the Array object
var swap = new Array();
function preload() {
// object detection to test if what we want works
if( document.images ) {
for( var j=0; j < arguments.length; j++ ) {
swap[j] = new Image(); // instance of image object
swap[j].src = arguments[j]; // arguments are handled on the fly
}
}
}
...
<body onload="preload('images/imgA.gif','images/imgB.gif');">
Simple javascript code for adjusting varying dimensions..
Code:
function imgOver(){
document.getElementById('img1').src = 'images/imgB.gif';
document.getElementById('img1').width = 205;
document.getElementById('img1').height = 87;
}
function imgOut(){
document.getElementById('img1').src = 'images/imgA.gif';
document.getElementById('img1').width = 24;
document.getElementById('imgfrog').height = 16;
}
Code:
<img id="img1" name="img1" src="images/imgA.gif" onmouseover="javascript:imgOver();" onmouseout="javascript:imgOut();" border="0">