I have this code for my button which randomizes urls from a list. But i have to refresh the page every time for the randomization to occur. I want it to redirect to a link everytime i click the button without refreshing the page.
Here is my code:
import wixLocation from ‘wix-location’;
$w.onReady( function (){
const allUrls = [
‘http://www.cameronsworld.net/’,
‘http://www.pictureofhotdog.com/’,
‘http://www.breadfish.nl/’,
‘http://www.catwig.com/’,
‘http://www.loopedforinfinity.com/’,
‘http://www.existentialcrisis.com/’,
‘http://www.gurka.se/’
];
const randomIndex = Math.floor(Math.random() * (allUrls.length - 1));
$w(‘#button1’).link = allUrls[randomIndex];
});
//Add your code for this event here:
export function button1_click(event) {
//Add your code for this event here:
console.log( $w(‘#button1’).link)
wixLocation.to($w(‘#button1’).link);
}
Hi if you define
const randomIndex = Math.floor(Math.random() * (allUrls.length - 1));$w(’ #button1 ').link = allUrls[randomIndex];
in your onClick function it should work
You added it in the onReady from your page so what it does is when the page loads, it generates a number and adds it to your button. if you add that code to your button, it will generate a new number evrytime you click on it.
Thanks for the answer. I am a non programmer and i had some help from wix support and they re-wrote my code. Could you help me what exactly i have to do? i put the
const randomIndex = Math.floor(Math.random() * (allUrls.length - 1));$w(’ #button1 ').link = allUrls[randomIndex];
in the onClick function but it says allUrls undifined.
can you send me the whole working code
Hi,
here is the code
import wixLocation from 'wix-location';
let allUrls = [
"http://www.cameronsworld.net",
"http://www.pictureofhotdog.com",
"http://www.breadfish.nl",
"http://www.catwig.com",
"http://www.loopedforinfinity.com",
"http://www.existentialcrisis.com",
"http://www.gurka.se"
];
export function button3_click(event) {
let randomIndex = Math.floor(Math.random() * (allUrls.length - 1));
$w("#button3").link = allUrls[randomIndex];
$w("#button3").link
wixLocation.to($w("#button3").link);
}
but to be able to view the website on button click you have to publish your website.
it won’t work in preview.
kristof.
Thank You sir.
But the it still doesn’t work. i changed the button 3 to 1 (as it is #button1 )
i published the site but still the button does nothing
Thanks so much for code! @volkaertskristof
I followed everything, and it’s poping me this
public/pages/mpi63.js: ‘import’ and ‘export’ may only appear at the top level
For others arriving here, you can also do this without writing code using free tools online:
https://www.allocate.monster/
https://www.nimblelinks.com/redirect-randomly
I think this would help me as I am trying something similar. I want users to scan a QR Code which will bring them to a blank “randomizer” page on wix and then automatically redirect them to a different page that was randomly selected. I too am not a coder and having a very hard time learning it.