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); }
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.
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
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.
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.