0

I try to embed a youtube video on a website and mute it.

I have already find this subject: How do you mute an embedded Youtube player?

So I have try the answer and it's work: https://jsfiddle.net/Jonathan_Ironman/BFDKS/9/

But when I try the same code outside of jsfiddle, the javascipt code is not execute anymore.

Source Code

<html>
    <head>
        <script type="text/javascript">
            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;

            function onYouTubeIframeAPIReady()
            {
                player = new YT.Player('ytplayer',
                {
                    events:
                    {
                        'onReady': onPlayerReady
                    }
                });
            }

            function onPlayerReady()
            {
                player.playVideo();
                // Mute!
                player.mute();
            }

        </script>

        <iframe id="ytplayer" type="text/html" style="position:absolute; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%; border:none; margin:0; padding:0; overflow:hidden; z-index:0;" src="https://www.youtube-nocookie.com/embed/DjxwLr6TjHs?playlist=DjxwLr6TjHs&enablejsapi=1&border=0&start=60&autoplay=1&showinfo=0&controls=0&disablekb=1&cc_load_policy=1&iv_load_policy=3&rel=0&loop=1" frameborder="0"></iframe>
    </head>

    <body>
    </body>
</html>

Any idea?

tirz
  • 2,041
  • 1
  • 22
  • 37

1 Answers1

3

Just make your first line of js look like this (copy&paste)

 tag.src = "http://www.youtube.com/iframe_api";

Otherwise (local) It will point to a file..

Maybe you just add a console output to check that the api is loaded correctly :)

Bennet G.
  • 537
  • 8
  • 14
  • That was just something that came into my mind ^^ I don't know why It works on jsfiddle.. Maybe they got something to make it automatic or so.. I don't know ^^But happy to hear that it worked :) Enjoy – Bennet G. Oct 28 '15 at 23:04