mardi 21 mai 2019

Play random video on page with javascript

I have three videos that I'd like to show randomly on each page load. I have the basic code which produces the correct URL, and it shows correctly in the page source with Google dev tools. However, the video won't play. If I hard code the URL the video plays fine, so it's almost as if the code needs to append the url before it executes the video?

Here's what I have so far:

var poster = document.querySelector('#videoposter');
var number = Math.floor(Math.random() * 3) + 1;
source.src += number +'.mp4';
poster.poster += number +'.jpg';

<video preload="none" playsinline="" loop="" muted="" autoplay="" onplaying="this.controls=false;document.getElementById('vidOverlay').style.display = 'none';" poster="/images/media/Poster-" id="videoposter">
<source id="homevideo" src="/images/media/Video-" type="video/mp4">
</video>




Aucun commentaire:

Enregistrer un commentaire