Web Analytics Made Easy -
StatCounter Embed random video - CodingForum

Announcement

Collapse
No announcement yet.

Embed random video

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Embed random video

    Hi, I'm using an indexibit template website and wanted to ask whether it is possible in html (alone) or perhaps with css to do the following:

    I am embedding video clips from Vimeo using standard embed codes directly into the html of a given page. Instead I would like the page to show a random video from a selection of 5 videos, which each have discrete urls, a single random video in same space on page each time the page is refreshed.

    Is this possible?

  • #2
    You might put each embed code in it's own container and then select a random one of them by Javascript and make it visible.

    Comment


    • #3
      Originally posted by Sempervivum View Post
      You might put each embed code in it's own container and then select a random one of them by Javascript and make it visible.
      I'm a bit of a newby so can I ask, do you mean this would be html containers? Could you give basic guides for js keywords or commands that I would need so I can research this?

      Many thanks

      Comment


      • #4
        This should do the job:
        Code:
                <style>
                    .embed {display: none;}
                </style>
                <div class="embed" id="embed1">
                    <iframe width="560" height="315" src="https://www.youtube.com/embed/lwgyAuAdiL0" frameborder="0" allowfullscreen></iframe>
                </div>
                <div class="embed" id="embed2">
                    <iframe width="560" height="315" src="https://www.youtube.com/embed/5dxrHmicrvE" frameborder="0" allowfullscreen></iframe>
                </div>
                <div class="embed" id="embed3">
                    <iframe width="560" height="315" src="https://www.youtube.com/embed/XndeRiwM0kQ" frameborder="0" allowfullscreen></iframe>
                </div>
                <script>
                    var videos = document.getElementsByClassName("embed");
                    var idx = Math.floor(Math.random() * videos.length);
                    videos[idx].style.display = "block";
                </script>
        I used Youtube videos but should work for vimeo embed codes as well.

        Comment

        Working...
        X