1

I have this script below to play random YouTube videos from a playlist. However I've found that it is not loading in IE/Edge, after I refresh the page (it does work the first time). Now I read something about a lazy load but I am not sure how to implement that in my script. I guess this is the best way to go to make sure the script is loaded correctly. Any other suggestions are welcome as well. Thanks.

    <?php
     $yt_id='PLFgquLnL59anYA8FwzqNFMp3KMcbKwMaT';
    ?>
    <div id="player"></div>
    <script>
            // 2. This code loads the IFrame Player API code asynchronously.
            var tag = document.createElement('script');

            tag.src = "//www.youtube.com/iframe_api";
            var firstScriptTag = document.getElementsByTagName('script')[0];
            firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

            var player;
            var numPl = Math.floor((Math.random() * 100) + 1);
            var playlistId = "<?php echo $yt_id; ?>";
            // 3. This function creates an <iframe> (and YouTube player)        
            function onYouTubeIframeAPIReady() {
                player = new YT.Player("player", {
                    height: '390',
                    width: '640',
                    playerVars: {
                    autoplay: 1,
                    loop: 1
        },
                    events: {
                        'onReady': onPlayerReady,
                        'onError': onPlayerError
                    }
                });
            }
            // onPlayerReady
            function onPlayerReady(event){
            //More player vars
            player.loadPlaylist( {
            listType: 'playlist',
            list: playlistId,
            index: numPl
        } );

            //Set shuffle   
            setTimeout(function() {
              player.setShuffle({'shufflePlaylist' : true});
                  }, 1000);
                }

            // onPlayerError
            function onPlayerError(){
                    player.nextVideo();
                    }
        </script>
abielita
  • 13,147
  • 2
  • 17
  • 59
RobbTe
  • 385
  • 4
  • 19
  • You may refer with this [page](https://social.msdn.microsoft.com/Forums/office/en-US/4f20d1eb-14f2-4b38-9a46-fe891a48bc42/embedded-youtube-issue-in-ie11?forum=sharepointdevelopmentprevious) for the embedded YouTube issue in IE11. Try to use `object` instead of `iframe`. You may also check the suggested action in this [link](https://social.msdn.microsoft.com/Forums/ie/en-US/b1b1fa69-da8b-494f-8586-f48fe4855c35/embeding-youtube-videos-with-an-iframe-doesnt-work-in-ie9?forum=iewebdevelopment). – abielita Mar 22 '18 at 17:13
  • Hi, thanks for your answer. Problem was due to loading the api source (www.youtube.com/iframe_api) twice, due to an old plugin. Works now. – RobbTe Mar 23 '18 at 17:15

0 Answers0