Ok, spero che vada (non l'ho provato)...
bannersList = array contenente i percorsi (relativi o assoluti) delle immagini.
anchorsList = array contenente gli href (relativi o assoluti) delle immagini.
Più sotto c'è una riga con "delayBanner = 5000; // delay (millis) of banners change": cambia il 5000 (5 secondi) con il valore che ti serve (è il ritardo di rotazione del banner).
L'ultimissima riga è "onload = appendBanners(dove);": cambia "dove" con il riferimento all'oggetto dove vuoi che compaia la tabella con i banner (mio consiglio: crea un div dove vuoi che appaiano i banner, attribuiscigli un id -es: divBanners- ed al posto di dove scrivi document.getElementById("divBanners").
codice:
var
bannersList = new Array
(
"banner1.ext",
"banner2.ext",
"banner3.ext",
"banner4.ext",
"banner5.ext"
),
anchorsList = new Array(
"href1",
"href2",
"href3",
"href4",
"href5"
),
firstBannerIndex,
secondBannerIndex,
firstBannerIndexNew,
secondBannerIndexNew;
function appendBanners(whereToAppend)
{
// variables
var
tableElement = document.createElement("table"),
tableBodyElement = document.createElement("tbody"),
tableRowElement = document.createElement("tr"),
tableData1Element = document.createElement("td"),
tableData2Element = document.createElement("td"),
anchor1 = document.createElement("a"),
anchor2 = document.createElement("a"),
imageBanner1 = document.createElement("img"),
imageBanner2 = document.createElement("img");
// parent-children relations
anchor1.appendChild(imageBanner1);
anchor2.appendChild(imageBanner2);
tableData1Element.appendChild(anchor1);
tableData2Element.appendChild(anchor2);
tableRowElement.appendChild(tableData1Element);
tableRowElement.appendChild(tableData2Element);
tableBodyElement.appendChild(tableRowElement);
tableElement.appendChild(tableBodyElement);
// images sources
firstBannerIndex = Math.floor(Math.random() * bannersList.length);
secondBannerIndex = Math.floor(Math.random() * bannersList.length);
if (firstBannerIndex == secondBannerIndex) secondBannerIndex = (secondBannerIndex + 1) % bannersList.length;
imageBanner1.setAttribute("src", bannersList[firstBannerIndex]);
imageBanner2.setAttribute("src", bannersList[secondBannerIndex]);
anchor1.setAttribute("href", anchorsList[firstBannerIndex]);
anchor2.setAttribute("href", anchorsList[secondBannerIndex]);
anchor1.setAttribute("target", "_blank");
anchor2.setAttribute("target", "_blank");
imageBanner1.style.border = imageBanner2.style.border = "0px";
anchor1.setAttribute("id", "aBanner1");
anchor2.setAttribute("id", "aBanner2");
var
delayBanner = 5000; // delay (millis) of banners change
setInterval("changeBannerAndHref('" + anchor1.getAttribute("id") + "', '" + anchor2.getAttribute("id") + "')", delayBanner);
whereToAppend.appendChild(tableElement);
}
function changeBannerAndHref(anchor1Id, anchor2Id)
{
firstBannerIndexNew = Math.floor(Math.random() * bannersList.length);
secondBannerIndexNew = Math.floor(Math.random() * bannersList.length);
if (firstBannerIndexNew == firstBannerIndex) firstBannerIndexNew = (firstBannerIndexNew + 1) % bannersList.length;
while (secondBannerIndexNew == secondBannerIndex || secondBannerIndexNew == firstBannerIndexNew)
{
secondBannerIndexNew = (secondBannerIndexNew + 1) % bannersList.length;
}
firstBannerIndex = firstBannerIndexNew;
secondBannerIndex = secondBannerIndexNew;
document.getElementById(anchor1Id).setAttribute("href", anchorsList[firstBannerIndex]);
document.getElementById(anchor2Id).setAttribute("href", anchorsList[secondBannerIndex]);
document.getElementById(anchor1Id).childNodes[0].setAttribute("src", bannersList[firstBannerIndex]);
document.getElementById(anchor2Id).childNodes[0].setAttribute("src", bannersList[secondBannerIndex]);
}
onload = function()
{
appendBanners(document.getElementById("appendHere"));
}
Spero che funzioni, fammi sapere se non va...