Skip to content Skip to sidebar Skip to footer

How To Make A Video Change At Specific Times Javascript

Hi guys i have a problem. I want a video link in a website to change its value each day randomly. Example: On Monday video link 1 plays. On Tuesday link 2 plays and so on. I want

Solution 1:

jsFiddle demo

var day = new Date().getDay();  
var video = document.getElementById('videorotator');
var videos =[
    "http//www.youtube.com/embed/UZopKnDHti8",
    "http://www.youtube.com/embed/z8GHNhteHJQ"
];

video.setAttribute('src', videos[day%videos.length]);   

You said you want one video/day, so get the day number and get the video src.


Solution 2:

If you want to display a video based on what day of the week it is, then I suggest you do it like this

var videos =["UZopKnDHti8","z8GHNhteHJQ","UZopKnDHti8","z8GHNhteHJQ","UZopKnDHti8","z8GHNhteHJQ","z8GHNhteHJQ"];
var day = new Date().getDay();

var yourElement = document.getElementById('videorotator');
yourElement.setAttribute( 'src', '//www.youtube.com/embed/' + videos[ day ] );

getDay returns 0 = sunday, 1 = monday, 2 = tuesday ... which is perfect for accessing an array

In the array I added just the id's, there's no need to repeat http://www.youtube.com/embed/ 7 times, might as well just append the ID to the url when you use setAttribute

DEMO

In the demo you can see the thumbnail flick from the original video to the correct one, simply remove the original src and it'll work smoother


Post a Comment for "How To Make A Video Change At Specific Times Javascript"