This script has been running over at waterbirds.org for years. Now it’s broken for some reason. It throws a “Cannot read property ‘src’ of undefined” error and I’ve tracked it down to preLoad[j].src (first line of runSlideShow function) as the problem because I can put a image link address there and it works. Any idea what’s causing the problem?
// Waterbird Society Theme for WordPress
// Rotating image banner applet
//
// Usage:
//<body onLoad='rotate_banner(slideShowSpeed, "Image1.jpg", "Image2.jpg"...)'>
var slideShowSpeed;
var j = 0;
var p;
var preLoad = new Array();
function rotate_banner() {
var args = rotate_banner.arguments;
slideShowSpeed = Number(args[0]);
p = args.length;
for (i = 0; i < p-1; i++) {
preLoad[i] = new Image();
preLoad[i].src = args[i+1];
}
runSlideShow();
}
function runSlideShow() {
document.images['SlideShow'].src = preLoad[j].src;
j++;
if (j > (preLoad.length - 1)) j = 0;
t = setTimeout('runSlideShow()', slideShowSpeed*1000);
}
Everything is alright with your code except for it is run globally so all
vardeclarations create global variables.The exact issue on waterbirds.org is that
jvariable is defined somewhere before and by the moment it comes topreLoad[j]jequals96, andpreload[96]is really undefined.Wrap your banner in anonymous self-executing function, for example, to make vars local, and you’re done:
And remember: global variables are evil! =)