mercredi 3 novembre 2021

Select randomly ONE item from a list of a htmls

Let's say I have several custom html snippets.

Is there a way on a Page to have different snippet picked randomly when the page loads.

I was thinking of JS that have the snippets and the code stored in single Custom-html-block. (worried that it wont work cause the snippets are js-html-string inside Custom-html block. how world escaping work ?)

Here is the idea in pseudo code :

<div id=rand />
let snippets = [ 's1...', 's2...', ...]
function pick(){ 
   let el = document.getElementById('rand')
   let item = random.choice(snippets)
   el.innerHTML = item
}
pick()

Is there a better way ?




Aucun commentaire:

Enregistrer un commentaire