How to have a random embed link displayed on my home page like random image?

I want set of embed link to be displayed. There should be a space where the content of embed link is displayed. And if the user refreshes the page, then the embed link changes every time. I did research in the forum, but was able to find code for random pic homepage which is:

import wixWindow from 'wix-window';
import wixData from 'wix-data';

$w.onReady(async function () {

 // Check the render cycle so this is just done once.
 // Otherwise, server-side will render, and then the image
 // switches to the image that the client-side rendered.
 if (wixWindow.rendering.renderCycle === 1) {
 let res = await wixData.query("AdvertisementImage").find();
 let items = res.items;
 let count = items.length;   // how many images do we have?
 let rnd = Math.floor(Math.random() * count);
        $w('#adimage').src = items[rnd].image;  // get a random image
    }
});

Update:
I am using the following code after editing the above code to suit me but it is still not working:


import wixWindow from 'wix-window';
import wixData from 'wix-data';

$w.onReady(async function () {

 // Check the render cycle so this is just done once.
 // Otherwise, server-side will render, and then the link
 // switches to the link that the client-side rendered.
 if (wixWindow.rendering.isNotEmpty('Quiz') === 1) {
 let res = await wixData.query("Quiz").find();
 let items = res.items;
 let count = items.length;   // how many links do we have?
 let rnd = Math.floor(Math.random() * count);
        $w('#html1').src = items[rnd].url;  // get a random link
    }
});

Help please

First of all, .renderCycle is depreciated (it always returns 1), so remove this if condition.
Second: add to the query .isNotEmpty(‘image’)
Make sure you use the correct collection name and field name.

What should be the source?
HTML Frame?

The collection image field key

I don’t want to display any image. I want to display embedded links on every refresh in a container.

Maybe the Random Image example will help.

That didn 't help. I tried copying it’s code and using it relevantly for my emebedded links, but didn’t work at all.

umm, I just noticed that you are trying to set an HtmlComponent to an image. That won’t work. You need to set an image element to an image.

If you want to set an HtmlComponent to an image, then you will need to write an embedded script that will receive a message that contains a link to the image, and then displays the image in the HtmlComponent. Your best option is to use an Image element.

Sorry I misunderstood what you were trying to do. You don’t need an HtmlComponent for this. It’s really easy, You can use a Button and set the link and/or the label . Or you can use a Text field for the link .