Najm
Najm

Reputation: 51

Make embedded YouTube Playlist start at random index

I've been searching for a way to make my embedded playlist start with a random video.

This is what I tried:

<iframe src="https://www.youtube.com/embed/videoseries?list=PLPmj00V6sF0s0k3Homcg1jkP0mLjddPgJ&index=<?php print(rand(1,11)) ?>?rel=0&autoplay=1&showinfo=0&controls=0&authide=0&iv_load_policy=3&?modestbranding=1" frameborder="0"  allowfullscreen></iframe>

Sadly, this does not work for some reason. Neither did it with echo. Other solutions (as well few on stackoverflow https://shrty.top/j) did not work either. Any ideas?

Upvotes: 0

Views: 3032

Answers (1)

Najm
Najm

Reputation: 51

Got it. This works:

<html>

<head>
    <script>
        var videos = ["https://www.youtube.com/embed/9bZkp7q19f0", "https://www.youtube.com/embed/dQw4w9WgXcQ"];
        window.onload = function () {
            var playerDiv = document.getElementById("random_player");
            var player = document.createElement("IFRAME");
            var randomVideoUrl = videos[Math.floor(Math.random() * videos.length)];
            player.setAttribute('width', '640');
            player.setAttribute('height', '390');
            player.setAttribute('src', randomVideoUrl);
            playerDiv.appendChild(player);
        };
    </script>
</head>

<body>
    <div id="random_player" />
</body>

</html>

Upvotes: 4

Related Questions