Random page loader

Hi guys,

it’s probably a simple thing, but i can’t think of any solution.

So i want my website to have a button that would load a random page from a list of pages on click. For example: i have different products A, B, and C, and i have them in different pages with unique URLs. The button click should load one of the products randomly.

Is there a way to do it?

Hello

I would suggest:

  • Creating an array that has all the links. (called Links for example)

  • Adding on click function to the button. (click on the button → properties panel → + onClick())

  • On the click function use random() to generate a random index, then get it’s link:

let link = Links[Math.floor(Math.random()*Links.length)];
  • Use wix location to link to the link you got randomly.
wixLocation.to(link);

Good Luck!
Massa

Thank You, Massa!

Although i know very little to none about coding, strangely it is kind of clear to me.

One thing left to figure out is how do i create an array, but i think i can find it on Google.

Thanks again!

Hi Massa,

would there be a way to automatically push the pages into the array? Lets say i have a page named “products” and i have pages “product1”, “product2” etc. under that page, and i want to push them in the array Links.

Is that possible?

Do you have the pages stored in the database?
If so that would be an easy approach, all you have to do is query your collection and get the products. for example:
if you have products field in your database

  • query the database
  • loop over the resulting items array
  • push each item’s product value in “links” in the right format (ex: ‘/home’)
import wixData from 'wix-data';
let Links=[]; // global value

// ...

wixData.query("myCollection")
  .find()
  .then( (results) => {
   results.items.forEach((item)={
     let link = "/"+ item.product
     Links.push(link)
   })

  } )
  .catch( (error) => {
    let errorMsg = error.message;
    let code = error.code;
  } );
  • randomly link to one of the item as explained before.
export function button1_click(event) {
  let link = Links[Math.floor(Math.random()*Links.length)];
  wixLocation.to(link);
}

I hope this helps!
Massa

I have not the stored in the database. The pages aren’t really product pages, they would be just some simple pages with some text in it and a link to another webpage.

How can i store my pages to the database?

so to make sure i understand what you want,
You have products, and for each product you have a page that you want to link to randomly?

  • the easiest way will be to create the array manually:

let Links = ['link1', 'link2' , 'link3'] 
  • or you can build a collection and follow that way. check this to know how to create a collection

Best
Massa

@massasalah18 Hi,

thank You for the help!

The concept is quite simpler: the pages would just contain an image, some text, and affiliate links. So they are not actual products, only affiliate links to them.

If there would be 5 pages, i wouldn’t mind writing them all manualy in an array, but what if i have thousands of pages? How can i get them all automatically stored in an array as links?

Also i would gradually create more pages, so i want them to be pushed in the array automatically, so that the random loader would be able to catch them too.

Thanks You again for Your help!

@kaikarisjonas where do you have your products stored? and have you considered using dynamic pages for your products?

would you please provide your website’s url.

Thanks

@massasalah18 The website is still only in my mind :slight_smile: i will create a demo, and post it here later today. Maybe it will be more clear what i trying to do.

As i said, i have no products, only affiliate links to products, so the only thing i need is to automatically push the URLs of all pages in my website to an array and then randomly load them on click.

@kaikarisjonas

Go ahead and get that idea rolling, and as you go you will find solutions and new ideas and if you face any problems post them on the forum!
you would want to check wixCode API documentations , examples , articles and Tutorials and get started :slight_smile:

Best of Luck!
Massa

@massasalah18 Hi Massa,

Sorry to bother You again.

In this code, at the line “let link = “/” +…” it says that the keyword “let” is reserved.

Is there a way to go around this?

@kaikarisjonas
Oh the problem is in this line:

results.items.forEach((item) = { 

it should have ‘>’ :

results.items.forEach((item) => {

Massa