# Create a random gallery each time reaload browser

**URL:** <https://forum.wixstudio.com/t/create-a-random-gallery-each-time-reaload-browser/11163>\
**Category:** Ask the community\
**Tags:** code, question\
**Created:** [May 17, 2023, 4:00pm UTC](https://forum.wixstudio.com/t/create-a-random-gallery-each-time-reaload-browser/11163 "2023-05-17T16:00:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![shin](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/shin/32/37426_2.png) [@shin](https://forum.wixstudio.com/u/shin)\
**Post date:** [May 17, 2023, 4:00pm UTC](https://forum.wixstudio.com/t/create-a-random-gallery-each-time-reaload-browser/11163/1 "2023-05-17T16:00:49Z")

</div>

Hey Guys,

So I’m working on a page, using a Pro Gallery Wix we want to show new pictures everytime we reload the browser or get back on the home page. If we click on the picture we open a link on a blank tab of the browser to the artist page.

Here is my code :

My Gallery is #galleryhome  
My data is #datagallery  
In the data I have :  
Image : for the picture in the Gallery  
Artiste : Name of the Artist  
URL : link of the page

import wixWindow **from** ‘wix-window’ ;  
import wixData **from** ‘wix-data’ ;

$w . onReady ( **async function** () {  
**if** ( wixWindow . rendering . env === ‘browser’ || wixWindow . viewMode === ‘Preview’ ) {  
**let** res = **await** wixData . query ( “datagallery” ). find ();  
**let** items = $w ( “#galleryhome” ). items ;  
**let** items = shuffle ( items );  
$w ( “#galleryhome” ). items = items ;  
}  
});  
export function shuffle ( array ) {  
**for** ( **let** i = array . length - 1 ; i \> 0 ; i --) {  
**const** j = Math . floor ( Math . random () \* ( i + 1 ));  
[array [ i], array [j]] = [array [ j], array [i]];  
}  
**return** array ;  
}

I can’t figure why my gallery don’t get randomize and why my links don’t work anymore…

![](https://us1.discourse-cdn.com/wix/original/3X/5/0/50ef5b9667b24bb42afa3d7d8ba275739b50675d.png)

I don’t found why I can’t use two times ‘items’ at line 7 and 8.

![](https://us1.discourse-cdn.com/wix/original/3X/e/7/e7538235924c60cf2027684573392207633703f3.png)

Thanks for helps.

---

<div class="post-metadata">

**Author:** ![Qu4dri](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/qu4dri/32/30158_2.png) [@Qu4dri](https://forum.wixstudio.com/u/Qu4dri)\
**Post date:** [May 17, 2023, 7:36pm UTC](https://forum.wixstudio.com/t/create-a-random-gallery-each-time-reaload-browser/11163/2 "2023-05-17T19:36:02Z")

</div>

Hi, the problem with the ‘let items’ is because you’re defining twice a variable with the same name.  
You can change for example the name of the second variable as ‘let shuffledItems’. Or just remove the ‘let’ keyword to overwrite the variable.

About your shuffling function i did not check if it actually works. I do not fully understand what your algorithm should do.

I suggest you another method for shuffling the items using the sort method:  
credits: [https://sebhastian.com/shuffle-array-javascript/](https://sebhastian.com/shuffle-array-javascript/)

```javascript
let shuffledItems = items.sort(function () { return Math.random() - 0.5;});

```

---

<div class="post-metadata">

**Author:** ![shin](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/shin/32/37426_2.png) [@shin](https://forum.wixstudio.com/u/shin)\
**Post date:** [May 18, 2023, 5:06pm UTC](https://forum.wixstudio.com/t/create-a-random-gallery-each-time-reaload-browser/11163/3 "2023-05-18T17:06:44Z")

</div>

Unfortunatly that doesn’t work, I tried to set a new item call “image” and put your fonction on it then set the value of the fonction to my data  
$w . onReady ( **async function** () {  
**if** ( wixWindow . rendering . env === ‘browser’ || wixWindow . viewMode === ‘Preview’ ) {  
**await** wixData . query ( “datagallery” ). find ();  
**let** image = $w ( “#galleryhome” ). items ;  
**let** shuffleItems = image . sort ( **function** () { **return** Math . random () - 0.5 ;});  
$w ( “#galleryhome” ). items = shuffleItems ;  
}  
});

---

<div class="post-metadata">

**Author:** ![Qu4dri](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/qu4dri/32/30158_2.png) [@Qu4dri](https://forum.wixstudio.com/u/Qu4dri)\
**Post date:** [May 19, 2023, 8:38am UTC](https://forum.wixstudio.com/t/create-a-random-gallery-each-time-reaload-browser/11163/4 "2023-05-19T08:38:02Z")

</div>

Hi, I actually have a gallery on my website and it works just fine with 1 line:

```javascript

$w.onReady(function () {

    $w('#gallery1').items = $w('#gallery1').items.sort(function () { return Math.random() - 0.5;});
});

```

Be sure that in your gallery there are more images that normally rotate. You don’t really need to query the dataset if the images are already saved inside the gallery.

---

<div class="post-metadata">

**Author:** ![Nicole\_Scott](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/nicole_scott/32/80832_2.png) [@Nicole\_Scott](https://forum.wixstudio.com/u/Nicole_Scott)\
**Post date:** [August 9, 2023, 7:35pm UTC](https://forum.wixstudio.com/t/create-a-random-gallery-each-time-reaload-browser/11163/5 "2023-08-09T19:35:46Z")

</div>

I just tried pasting that code in the Velo area, but the preview shows the gallery entirely blank. I looked in the inspector and don’t see anything for #gallery1. Not sure what to replace the #gallery1 with, but I’m thinking that’s the issue. Can anyone help clarify how to get this solution working for a randomized gallery slider?

---

<div class="post-metadata">

**Author:** ![noahlovell](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/noahlovell/32/118920_2.png) [@noahlovell](https://forum.wixstudio.com/u/noahlovell)\
**Post date:** [August 9, 2023, 7:37pm UTC](https://forum.wixstudio.com/t/create-a-random-gallery-each-time-reaload-browser/11163/6 "2023-08-09T19:37:20Z")

</div>

Are you able to share a screenshot of the code you have? Are you seeing any red lines under any of the code?
