javascript - Youtube Player API is slower than website load -


i have website has index page lot of text , button triggers modal window, in may see youtube video. in normal scenario (people reads text of page, realize button, click button) there's no problem, when click button after page loads (or during...) modal window appears no video in it, of course firebug throws error saying video isn't loaded yet.

i don't mind waiting video load after pressing button, i'd push button , 100% of times watch video, don't know how start loading api in moment want. there have relevant pieces of code:

var player; function onyoutubeplayerapiready() {     player = new yt.player("player", {         height: $('#modalvideo').height()*0.9,         width: $('#modalvideo').width()*0.9,         videoid: "neetgjp5-ag"     }) }  $(document).ready(function() {     $(".youtube").on("click", function(e) {         e.preventdefault();         // place want load youtube api, if have wait         $("#modalvideo").modal("show");         player.playvideo();     }); }); 

thank everybody!

something maybe (sorry not seem work inside stackoverflow's snippet iframe - can try working jsfiddle instead: https://jsfiddle.net/hzmdj2m6/)?

var player;  var playwhenready = false;    var tag = document.createelement('script');  tag.src = "//www.youtube.com/iframe_api";  var firstscripttag = document.getelementsbytagname('script')[0];  firstscripttag.parentnode.insertbefore(tag, firstscripttag);    function onyoutubeiframeapiready() {    console.log('api ready');    player = new yt.player("player", {      height: $('#modalvideo').height() * 0.9,      width: $('#modalvideo').width() * 0.9,      videoid: "neetgjp5-ag",      playervars: {        origin: 'http://stacksnippets.net'      },      events: {        onready: onyoutubeplayerready      }    })  }    function onyoutubeplayerready(e) {    console.log('player ready');    player.isready = true;    if (playwhenready) {      playvideo();    }  }    function playvideo() {    console.log('playvideo');    if (player && player.playvideo) {      console.log('now');      //$("#modalvideo").modal("show");      player.playvideo();      return;    } else {      console.log('postpone');      playwhenready = true;    }  }    $(document).ready(function() {    $(".youtube").on("click", function(e) {      e.preventdefault();      playvideo();    });  });
#modalvideo {    width: 300px;    height: 300px;    background: red;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <a href="#" class="youtube">play</a>  <div id="modalvideo">      <div id="player"></div>  </div>

or use queueing: https://developers.google.com/youtube/iframe_api_reference#queueing_functions


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -