Sebastian Becker: cross browser random pictures

Beitrag lesen

Hallo,

ich möchte gerne auf einer Seite ein Zufallsbild einbinden.
(Test --> http://www.incontri-berlin.de/index_js.htm)

Folgendes Skript funktioniert im IE 5.5, führt aber auf manchen anderen Browsern bzw. Versionen, die ich leider nicht auf meinem Rechner installiert habe, zur Ausgabe von "undefined":

--------------------------------------------------------------

// Zahl der Bilder hier konfigurieren
var picCount = 5;

// Standard-alt-Text hier konfigurieren
var defaultText = 'INCONTRI - Ristorante Italiano';

randPic = new Array;
picWidth = new Array;
picHeight = new Array;
picTxt = new Array;
altTxt = new Array;

// Array mit Bildnamen hier konfigurieren
randPic[0]   = 'restaurant2.jpg';
picWidth[0]  = '240';
picHeight[0] = '160';
picTxt[0]    = 'Ristorante';

randPic[1]   = 'nudelgabel.gif';
picWidth[1]  = '120';
picHeight[1] = '180';
picTxt[1]    = 'Maccaroni & Gabel';

randPic[2]   = 'bar1.jpg';
picWidth[2]  = '180';
picHeight[2] = '120';
picTxt[2]    = 'Bar';

randPic[3]   = 'osteria.jpg';
picWidth[3]  = '180';
picHeight[3] = '120';
picTxt[3]    = 'C. Bloch, In un osteria romana, 1860';

randPic[4]   = 'gabel.jpg';
picWidth[4]  = '180';
picHeight[4] = '120';
picTxt[4]    = 'Olive & Gabel';

// ---------- Ab hier nichts mehr verändern! ----------

// Standard-alt-Text
for (i = 0; i < picCount; i++)
{
if ( picTxt[i] == '' || picTxt[i] == undefined ) altTxt[i] = defaultText;
else altTxt[i] = picTxt[i];
}

// Zufallsfunktion
var num = Math.round((picCount - 1) * Math.random());

// Ausgabe des HTML-Quelltextes
function ranPic()
{
var str = '<a href="index.htm" onMouseOver="window.status='';return true;" onMouseOut="window.status='';return true;"><img src="img/' + randPic[num] + '" width="' + picWidth[num] + '" height="' + picHeight[num] + '" border="0" alt="' + altTxt[num] + '"></a>';
document.write(str);
// alert(str); // test
}

--------------------------------------------------------------

Ich kann den Fehler beim besten Willen nicht finden. Vielleicht kann mir jemand weiterhelfen?!

Mit Dank und Grüßen aus Berlin,

Sebastian