Button Link Rotation? (Random Link Button)

I was wondering if it was possible to use wix code to change what a button links to each time the website is loaded?

Example.
Random Link Button is clicked and takes user to new url.
User returns to the website, clicks random link button again, and is taken to a different url than last time.

I want to be able to cycle through a bunch of different urls.

3 Likes

Yes, that’s possible. Look here: Button - Velo API Reference - Wix.com

I can’t figure out how do even go about starting this. Could someone give an example?

Hello YoYo, I’m trying to accomplish the same thing. have you found a way to do it?

Hi,
You can do this using code.
I’ll give an example for a random linking (and not a rotational one).
Have an array with all the urls, and set the button link when the page is loaded.
Read here about having a random integer in a range.

Let say I have a button with the id of ‘button1’.

const allUrls = [
    'www.google.com',
    'www.facebook.com',
    'www.cnn.com',
    'www.abc.com'
];

$w.onReady(function(){
    const randomIndex = Math.floor(Math.random() * (allUrls.length - 1));
    $w('#button1').link = allUrls[randomIndex];
});

Liran.

Thank you i’ll try that!

I’m sorry but I can’t code, it says there’s a missing semicolon? But when I add one It says unexpected token?

I’ve updated the code. try again.

Thank you no errors anymore! Do I have to link the code to the button or anything? When I try it it doesn’t send me to any page?

Did you put your button id where mine says ‘#button1’ ?

Yes! I just had to change it to #button2. Can the problem be that the button is inside a slideshow?


.

Oh, my bad, try adding ‘http://’ or ‘https://’ to the links:

const allUrls = [
    'https://www.google.com',
    'https://www.facebook.com',
    'https://www.cnn.com',
    'https://www.abc.com'
];

Liran.

Thank you so much! It works :smiley:

hi :slight_smile: i did the same code however it keeps on opening one page of the 4

Could it be that the randomize function returnns 4 a couple of times? That IS possible. Try to console.log the randomIndex.

Sorry to bump this thread, but I’ve been following the advice here and I’m also only getting one page coming up when I press the button. It’s not randomising at all. Any more advice?

Im having the same problem. Any advice?

same