Question:
How do I make a video play at a random second every time the page is refreshed
Product:
Wix Studio Editor
What are you trying to achieve:
I have a banner film playing on my homepage. I want it to start at any random second when you refresh the page. At the moment it plays in loop at starts at the beginning every time, I want it to always be random.
What have you already tried:
I’ve tried ChatGPT recommended Jave code
Additional information:
I’m basically trying to stop the same first 5 secs always being the same every time you land on the page. It becomes very repetitive.
1 Like
Try this one…
$w.onReady(function () {
const video = $w("#videoPlayer1"); console.log(video);
const duration = video.duration; console.log('Duration: ', duration);
if (duration && duration > 0) {
const randomSecond = Math.floor(Math.random() * duration); console.log('Random-Seconds: ', randomSecond)
video.seek(randomSecond);
video.play();
} else {console.log('No VIDEO loaded!!!');}
})
- Paste this code onto your page, where your videoPlayer is located at.
- To be able to use codings → first activate the DEV-MODE in your Wix-Editor.
- Publish and open the published site.
- Give me a feedback and like it

HOLY-MOLY!!! → Did not work right? 
Well this is called → CYBER-SECURITY ←
In modern webbrowsers you can’t do → REAL AUTOPLAY ← anymore, it is restricted.
But since CODE-NINJA is a real NINJA
, let’s do a tricky WORK-AROUND 
Now remove the old one and paste this one…
$w.onReady(()=> {
const video = $w("#videoPlayer1"); console.log(video); video.mute();
const duration = video.duration; console.log('Duration: ', duration);
if (duration && duration > 0) {console.log('GO');
const randomSecond = Math.floor(Math.random() * duration); console.log('Random-Seconds: ', randomSecond)
video.seek(randomSecond);
video.play();
} else {console.log('No VIDEO loaded!!!');}
})
And again → HOLY-Sh…!!! Working?
What was the trick??? 