onPlayerReady() is not firing, player is only partially initialized












0















When using the Youtube iframe API, onPlayerReady is never called, and the players are only partially loaded.



I have tried some things from other posts:



Here two solutions were proposed:




  • Setting the origin variable to match the server domain: I tried to set it both in the iframe src attribute in the HTML tag, and in playerVars when initializing the player. No changes seem to have happened.



  • Replacing <iframe> for <div> tags: This did work in other projects, but not here. Instead I get an error I never had before, when javascript tries to create the player:



    Uncaught TypeError: tubeId.indexOf is not a function
    at returnFn (mytube.js:3115)
    at Xg.c [as R] (www-embed-player.js:519)
    at gh (www-embed-player.js:513)
    at dh (www-embed-player.js:512)
    at Xg.a.w (www-embed-player.js:509)
    at $g (www-embed-player.js:510)
    at Xg.h.da (www-embed-player.js:505)
    at www-embed-player.js:533



The site has two players, and although I only replaced the tags in one of them, both players started throwing this error. Also, the player variables returned undefined now.



This post says the solution is setting enablejsapi to 1, but I already did so in the HTML iframe tag.



These errors are not happening in all computers. All of them were detected on a Windows 8 machine running Google Chrome (couldn't test on other browsers). The same happened in google Chrome on MacOS (although I couldn't test the last changes which triggered the mytube.js error there).
In my computer (windows 7, tested the site on Chrome and Firefox) everything works mostly well, except for an occasional "Failed to execute 'postMessage' on 'DOMWindow'" error (it says the player origin is set to http://www.youtube.com, although I already replaced it). This error doesn't pop up always, but maybe 30% of the times.



This is how I create the players:



HTML:



<iframe id="video1" type="text/html" src="https://www.youtube.com/embed/?rel=0&enablejsapi=1&origin=https://mywebsite.com"></iframe>
<!-- Video ID is loaded on user interaction, using loadVideoById() -->


JS/jQuery:



$(document).ready(function() {
loadYouTubeAPI();
});


function loadYouTubeAPI(){
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
}

function onYouTubeIframeAPIReady() {
player = new YT.Player('video1', {
playerVars: {'origin':'https://mywebsite.com/'},
events: {
'onReady': onPlayerReady1
},
});
}

function onPlayerReady1(event) { //I have different players that do different things when ready; I need different names for each onPlayerReady function
(...)
}


Here are screenshots of how the console returns the player variables: left is how the players are being created now, right is how they should be. In the left image the player is missing all of the methods, so I cannot control it.










share|improve this question



























    0















    When using the Youtube iframe API, onPlayerReady is never called, and the players are only partially loaded.



    I have tried some things from other posts:



    Here two solutions were proposed:




    • Setting the origin variable to match the server domain: I tried to set it both in the iframe src attribute in the HTML tag, and in playerVars when initializing the player. No changes seem to have happened.



    • Replacing <iframe> for <div> tags: This did work in other projects, but not here. Instead I get an error I never had before, when javascript tries to create the player:



      Uncaught TypeError: tubeId.indexOf is not a function
      at returnFn (mytube.js:3115)
      at Xg.c [as R] (www-embed-player.js:519)
      at gh (www-embed-player.js:513)
      at dh (www-embed-player.js:512)
      at Xg.a.w (www-embed-player.js:509)
      at $g (www-embed-player.js:510)
      at Xg.h.da (www-embed-player.js:505)
      at www-embed-player.js:533



    The site has two players, and although I only replaced the tags in one of them, both players started throwing this error. Also, the player variables returned undefined now.



    This post says the solution is setting enablejsapi to 1, but I already did so in the HTML iframe tag.



    These errors are not happening in all computers. All of them were detected on a Windows 8 machine running Google Chrome (couldn't test on other browsers). The same happened in google Chrome on MacOS (although I couldn't test the last changes which triggered the mytube.js error there).
    In my computer (windows 7, tested the site on Chrome and Firefox) everything works mostly well, except for an occasional "Failed to execute 'postMessage' on 'DOMWindow'" error (it says the player origin is set to http://www.youtube.com, although I already replaced it). This error doesn't pop up always, but maybe 30% of the times.



    This is how I create the players:



    HTML:



    <iframe id="video1" type="text/html" src="https://www.youtube.com/embed/?rel=0&enablejsapi=1&origin=https://mywebsite.com"></iframe>
    <!-- Video ID is loaded on user interaction, using loadVideoById() -->


    JS/jQuery:



    $(document).ready(function() {
    loadYouTubeAPI();
    });


    function loadYouTubeAPI(){
    var tag = document.createElement('script');
    tag.src = "https://www.youtube.com/iframe_api";
    var firstScriptTag = document.getElementsByTagName('script')[0];
    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
    }

    function onYouTubeIframeAPIReady() {
    player = new YT.Player('video1', {
    playerVars: {'origin':'https://mywebsite.com/'},
    events: {
    'onReady': onPlayerReady1
    },
    });
    }

    function onPlayerReady1(event) { //I have different players that do different things when ready; I need different names for each onPlayerReady function
    (...)
    }


    Here are screenshots of how the console returns the player variables: left is how the players are being created now, right is how they should be. In the left image the player is missing all of the methods, so I cannot control it.










    share|improve this question

























      0












      0








      0








      When using the Youtube iframe API, onPlayerReady is never called, and the players are only partially loaded.



      I have tried some things from other posts:



      Here two solutions were proposed:




      • Setting the origin variable to match the server domain: I tried to set it both in the iframe src attribute in the HTML tag, and in playerVars when initializing the player. No changes seem to have happened.



      • Replacing <iframe> for <div> tags: This did work in other projects, but not here. Instead I get an error I never had before, when javascript tries to create the player:



        Uncaught TypeError: tubeId.indexOf is not a function
        at returnFn (mytube.js:3115)
        at Xg.c [as R] (www-embed-player.js:519)
        at gh (www-embed-player.js:513)
        at dh (www-embed-player.js:512)
        at Xg.a.w (www-embed-player.js:509)
        at $g (www-embed-player.js:510)
        at Xg.h.da (www-embed-player.js:505)
        at www-embed-player.js:533



      The site has two players, and although I only replaced the tags in one of them, both players started throwing this error. Also, the player variables returned undefined now.



      This post says the solution is setting enablejsapi to 1, but I already did so in the HTML iframe tag.



      These errors are not happening in all computers. All of them were detected on a Windows 8 machine running Google Chrome (couldn't test on other browsers). The same happened in google Chrome on MacOS (although I couldn't test the last changes which triggered the mytube.js error there).
      In my computer (windows 7, tested the site on Chrome and Firefox) everything works mostly well, except for an occasional "Failed to execute 'postMessage' on 'DOMWindow'" error (it says the player origin is set to http://www.youtube.com, although I already replaced it). This error doesn't pop up always, but maybe 30% of the times.



      This is how I create the players:



      HTML:



      <iframe id="video1" type="text/html" src="https://www.youtube.com/embed/?rel=0&enablejsapi=1&origin=https://mywebsite.com"></iframe>
      <!-- Video ID is loaded on user interaction, using loadVideoById() -->


      JS/jQuery:



      $(document).ready(function() {
      loadYouTubeAPI();
      });


      function loadYouTubeAPI(){
      var tag = document.createElement('script');
      tag.src = "https://www.youtube.com/iframe_api";
      var firstScriptTag = document.getElementsByTagName('script')[0];
      firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
      }

      function onYouTubeIframeAPIReady() {
      player = new YT.Player('video1', {
      playerVars: {'origin':'https://mywebsite.com/'},
      events: {
      'onReady': onPlayerReady1
      },
      });
      }

      function onPlayerReady1(event) { //I have different players that do different things when ready; I need different names for each onPlayerReady function
      (...)
      }


      Here are screenshots of how the console returns the player variables: left is how the players are being created now, right is how they should be. In the left image the player is missing all of the methods, so I cannot control it.










      share|improve this question














      When using the Youtube iframe API, onPlayerReady is never called, and the players are only partially loaded.



      I have tried some things from other posts:



      Here two solutions were proposed:




      • Setting the origin variable to match the server domain: I tried to set it both in the iframe src attribute in the HTML tag, and in playerVars when initializing the player. No changes seem to have happened.



      • Replacing <iframe> for <div> tags: This did work in other projects, but not here. Instead I get an error I never had before, when javascript tries to create the player:



        Uncaught TypeError: tubeId.indexOf is not a function
        at returnFn (mytube.js:3115)
        at Xg.c [as R] (www-embed-player.js:519)
        at gh (www-embed-player.js:513)
        at dh (www-embed-player.js:512)
        at Xg.a.w (www-embed-player.js:509)
        at $g (www-embed-player.js:510)
        at Xg.h.da (www-embed-player.js:505)
        at www-embed-player.js:533



      The site has two players, and although I only replaced the tags in one of them, both players started throwing this error. Also, the player variables returned undefined now.



      This post says the solution is setting enablejsapi to 1, but I already did so in the HTML iframe tag.



      These errors are not happening in all computers. All of them were detected on a Windows 8 machine running Google Chrome (couldn't test on other browsers). The same happened in google Chrome on MacOS (although I couldn't test the last changes which triggered the mytube.js error there).
      In my computer (windows 7, tested the site on Chrome and Firefox) everything works mostly well, except for an occasional "Failed to execute 'postMessage' on 'DOMWindow'" error (it says the player origin is set to http://www.youtube.com, although I already replaced it). This error doesn't pop up always, but maybe 30% of the times.



      This is how I create the players:



      HTML:



      <iframe id="video1" type="text/html" src="https://www.youtube.com/embed/?rel=0&enablejsapi=1&origin=https://mywebsite.com"></iframe>
      <!-- Video ID is loaded on user interaction, using loadVideoById() -->


      JS/jQuery:



      $(document).ready(function() {
      loadYouTubeAPI();
      });


      function loadYouTubeAPI(){
      var tag = document.createElement('script');
      tag.src = "https://www.youtube.com/iframe_api";
      var firstScriptTag = document.getElementsByTagName('script')[0];
      firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
      }

      function onYouTubeIframeAPIReady() {
      player = new YT.Player('video1', {
      playerVars: {'origin':'https://mywebsite.com/'},
      events: {
      'onReady': onPlayerReady1
      },
      });
      }

      function onPlayerReady1(event) { //I have different players that do different things when ready; I need different names for each onPlayerReady function
      (...)
      }


      Here are screenshots of how the console returns the player variables: left is how the players are being created now, right is how they should be. In the left image the player is missing all of the methods, so I cannot control it.







      javascript youtube-iframe-api






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 31 '18 at 14:13









      M. CortésM. Cortés

      1




      1
























          3 Answers
          3






          active

          oldest

          votes


















          0














          Try setting ' enablejsapi="1" ' both in the url and as and attribute of the iframe like this:



          <iframe id="video1" type="text/html" src="https://www.youtube.com/embed/?rel=0&enablejsapi=1&origin=https://mywebsite.com" enablejsapi="1"></iframe>





          share|improve this answer
























          • I updated it, but cannot test it right away. By the way, this is the website: miriamhecht.com.ar/#obra_01. That link should trigger the errors if they are present

            – M. Cortés
            Dec 31 '18 at 15:47





















          0














          remove the origin:https://mywebsite.com/ from the URL and playerVars: {'origin':'https://mywebsite.com/'} block while developing. Keep in mind the following in production:



          From the
          YouTube Player API Reference for iframe Embeds




          As an extra security measure, you should also include the origin parameter to the URL, specifying the URL scheme (http:// or https://) and full domain of your host page as the parameter value. While origin is optional, including it protects against malicious third-party JavaScript being injected into your page and hijacking control of your YouTube player.







          share|improve this answer
























          • I added those when I uploaded the website to the server, as I couldn't make the players work at all without setting the origin in at least one of those places. Could this be causing problems?

            – M. Cortés
            Dec 31 '18 at 15:36



















          0














          Update: I have found that the error linked to mytube.js and www-embed-player.js is due to code injected by a Chrome extension (SmartVideo for YouTube); I still have to figure out why, but somehow this extension prevents the players from loading correctly. Disabling it and replacing iframe tags by div tags seems to have solved the problem.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53988421%2fonplayerready-is-not-firing-player-is-only-partially-initialized%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Try setting ' enablejsapi="1" ' both in the url and as and attribute of the iframe like this:



            <iframe id="video1" type="text/html" src="https://www.youtube.com/embed/?rel=0&enablejsapi=1&origin=https://mywebsite.com" enablejsapi="1"></iframe>





            share|improve this answer
























            • I updated it, but cannot test it right away. By the way, this is the website: miriamhecht.com.ar/#obra_01. That link should trigger the errors if they are present

              – M. Cortés
              Dec 31 '18 at 15:47


















            0














            Try setting ' enablejsapi="1" ' both in the url and as and attribute of the iframe like this:



            <iframe id="video1" type="text/html" src="https://www.youtube.com/embed/?rel=0&enablejsapi=1&origin=https://mywebsite.com" enablejsapi="1"></iframe>





            share|improve this answer
























            • I updated it, but cannot test it right away. By the way, this is the website: miriamhecht.com.ar/#obra_01. That link should trigger the errors if they are present

              – M. Cortés
              Dec 31 '18 at 15:47
















            0












            0








            0







            Try setting ' enablejsapi="1" ' both in the url and as and attribute of the iframe like this:



            <iframe id="video1" type="text/html" src="https://www.youtube.com/embed/?rel=0&enablejsapi=1&origin=https://mywebsite.com" enablejsapi="1"></iframe>





            share|improve this answer













            Try setting ' enablejsapi="1" ' both in the url and as and attribute of the iframe like this:



            <iframe id="video1" type="text/html" src="https://www.youtube.com/embed/?rel=0&enablejsapi=1&origin=https://mywebsite.com" enablejsapi="1"></iframe>






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 31 '18 at 14:45









            jorgeRamirezjorgeRamirez

            213




            213













            • I updated it, but cannot test it right away. By the way, this is the website: miriamhecht.com.ar/#obra_01. That link should trigger the errors if they are present

              – M. Cortés
              Dec 31 '18 at 15:47





















            • I updated it, but cannot test it right away. By the way, this is the website: miriamhecht.com.ar/#obra_01. That link should trigger the errors if they are present

              – M. Cortés
              Dec 31 '18 at 15:47



















            I updated it, but cannot test it right away. By the way, this is the website: miriamhecht.com.ar/#obra_01. That link should trigger the errors if they are present

            – M. Cortés
            Dec 31 '18 at 15:47







            I updated it, but cannot test it right away. By the way, this is the website: miriamhecht.com.ar/#obra_01. That link should trigger the errors if they are present

            – M. Cortés
            Dec 31 '18 at 15:47















            0














            remove the origin:https://mywebsite.com/ from the URL and playerVars: {'origin':'https://mywebsite.com/'} block while developing. Keep in mind the following in production:



            From the
            YouTube Player API Reference for iframe Embeds




            As an extra security measure, you should also include the origin parameter to the URL, specifying the URL scheme (http:// or https://) and full domain of your host page as the parameter value. While origin is optional, including it protects against malicious third-party JavaScript being injected into your page and hijacking control of your YouTube player.







            share|improve this answer
























            • I added those when I uploaded the website to the server, as I couldn't make the players work at all without setting the origin in at least one of those places. Could this be causing problems?

              – M. Cortés
              Dec 31 '18 at 15:36
















            0














            remove the origin:https://mywebsite.com/ from the URL and playerVars: {'origin':'https://mywebsite.com/'} block while developing. Keep in mind the following in production:



            From the
            YouTube Player API Reference for iframe Embeds




            As an extra security measure, you should also include the origin parameter to the URL, specifying the URL scheme (http:// or https://) and full domain of your host page as the parameter value. While origin is optional, including it protects against malicious third-party JavaScript being injected into your page and hijacking control of your YouTube player.







            share|improve this answer
























            • I added those when I uploaded the website to the server, as I couldn't make the players work at all without setting the origin in at least one of those places. Could this be causing problems?

              – M. Cortés
              Dec 31 '18 at 15:36














            0












            0








            0







            remove the origin:https://mywebsite.com/ from the URL and playerVars: {'origin':'https://mywebsite.com/'} block while developing. Keep in mind the following in production:



            From the
            YouTube Player API Reference for iframe Embeds




            As an extra security measure, you should also include the origin parameter to the URL, specifying the URL scheme (http:// or https://) and full domain of your host page as the parameter value. While origin is optional, including it protects against malicious third-party JavaScript being injected into your page and hijacking control of your YouTube player.







            share|improve this answer













            remove the origin:https://mywebsite.com/ from the URL and playerVars: {'origin':'https://mywebsite.com/'} block while developing. Keep in mind the following in production:



            From the
            YouTube Player API Reference for iframe Embeds




            As an extra security measure, you should also include the origin parameter to the URL, specifying the URL scheme (http:// or https://) and full domain of your host page as the parameter value. While origin is optional, including it protects against malicious third-party JavaScript being injected into your page and hijacking control of your YouTube player.








            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 31 '18 at 15:19









            1cgonza1cgonza

            1,321617




            1,321617













            • I added those when I uploaded the website to the server, as I couldn't make the players work at all without setting the origin in at least one of those places. Could this be causing problems?

              – M. Cortés
              Dec 31 '18 at 15:36



















            • I added those when I uploaded the website to the server, as I couldn't make the players work at all without setting the origin in at least one of those places. Could this be causing problems?

              – M. Cortés
              Dec 31 '18 at 15:36

















            I added those when I uploaded the website to the server, as I couldn't make the players work at all without setting the origin in at least one of those places. Could this be causing problems?

            – M. Cortés
            Dec 31 '18 at 15:36





            I added those when I uploaded the website to the server, as I couldn't make the players work at all without setting the origin in at least one of those places. Could this be causing problems?

            – M. Cortés
            Dec 31 '18 at 15:36











            0














            Update: I have found that the error linked to mytube.js and www-embed-player.js is due to code injected by a Chrome extension (SmartVideo for YouTube); I still have to figure out why, but somehow this extension prevents the players from loading correctly. Disabling it and replacing iframe tags by div tags seems to have solved the problem.






            share|improve this answer




























              0














              Update: I have found that the error linked to mytube.js and www-embed-player.js is due to code injected by a Chrome extension (SmartVideo for YouTube); I still have to figure out why, but somehow this extension prevents the players from loading correctly. Disabling it and replacing iframe tags by div tags seems to have solved the problem.






              share|improve this answer


























                0












                0








                0







                Update: I have found that the error linked to mytube.js and www-embed-player.js is due to code injected by a Chrome extension (SmartVideo for YouTube); I still have to figure out why, but somehow this extension prevents the players from loading correctly. Disabling it and replacing iframe tags by div tags seems to have solved the problem.






                share|improve this answer













                Update: I have found that the error linked to mytube.js and www-embed-player.js is due to code injected by a Chrome extension (SmartVideo for YouTube); I still have to figure out why, but somehow this extension prevents the players from loading correctly. Disabling it and replacing iframe tags by div tags seems to have solved the problem.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 4 at 0:04









                M. CortésM. Cortés

                1




                1






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53988421%2fonplayerready-is-not-firing-player-is-only-partially-initialized%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas