Potresti crearti una funzione javascript che punta al tuo elemento DOM. (id ="banner" nel mio caso)
codice:
const banner = document.getElementById('banner')
// dati ritornati da una ipotetica fetch / ajax
const objData = [
{
src: 'img1.jpg',
url: 'https://www.google.it'
},
{
src: 'img2.jpg',
url: 'https://www.ansa.it'
}
]
const bannerRnd = (obj) => {
const rnd = Math.random() * obj.length | 0
const data = obj[rnd]
const link = document.createElement('a')
const image = document.createElement('img')
link.setAttribute('href', data.url)
link.setAttribute('target', '_blank')
image.setAttribute('src', data.src)
link.appendChild(image)
banner.appendChild(link)
}
bannerRnd(objData)