Random Homepage Load

Hi all. I am trying to build a mixed webpage on wix, that features both photography and film. Does anyone know of a way to have two complete homepages that load randomly on site visit. So one with a video showreel and one with a photo carousel.

Thanks for the help!

Hello antjar,

check this out…

$w.onReady(function () {
    console.log(getRandomInt(1));
});

export function button1_click(event) {
    console.log(getRandomInt(1));
}

function getRandomInt(max) {
 return Math.floor(Math.random() * Math.floor(max));
}

This function will jump between the value “0” and “1”, every time you press on button1, or loads your page (the page where you put in this code).

You can see the results in the console-log.

So your idea could be…
0 = page-A
1 = page-B

Hi :raised_hand_with_fingers_splayed:

You can get a random number as russian-dima demonstrated, then redirect visitors to the relevant page accordingly.

To so, you need to import the Wix Location API module first.

import wixLocation from 'wix-location';

Then on the page’s onReady() function, get a random number:

let num = Math.floor(Math.random * 20);

It will give you a number between 0 and 2 - this will create equal chances for each page - then redirect visitors accordingly

if (num > 0 && num <= 1) {
    wixLoation.to(/photography) // photography home page 
} else {
    wixLocation.to(/films) // films home page
}

To put everything together

import wixLocation from 'wix-location';

$w.onReady(async () => {
    let num = Math.floor(Math.random * 20);
    
    if (num > 0 && num <= 1) {
        await wixLoation.to(/photography) // photography home page
    } else {
        await wixLocation.to(/films) // films home page
    }    
})

NOTE: You should include this an empty home page in the Page code section.

Hope that helped~!
Ahmad