youtube javascript api - Waiting for both YouTubePlayerAPI and JQuery Document to be ready -


i seem have race condition in code. loading youtube player api asynchronously via:

  <script>     var tag = document.createelement('script');     tag.src = "https://www.youtube.com/player_api";     var firstscripttag = document.getelementsbytagname('script')[0];     firstscripttag.parentnode.insertbefore(tag, firstscripttag);   </script> 

and using coffeescript have:

$ ->   window.onyoutubeplayerapiready = () ->     #use youtube player api 

sometimes onyoutubeplayerapiready() method fires before jquery document ready method , in case none of youtube player code runs. have experience or suggestions handling this?

edit: based on feedback (including example code) op (@wuliwong) following solution resolves race condition original hinted @ in op's question:

$(document).bind 'custom-ready', () ->       ... custom initialization code ...   # ensure 'custom-ready' called when both document ,  # youtube player api ready. order occur in not  # guarenteed following code caters both scenarios.  # cater document ready first scenario $ ->      window.onyoutubeplayerapiready = () ->          $(document).trigger('custom-ready')   # cater youtube player api ready first scenario window.onyoutubeplayerapiready = () ->      $ ->          $(document).trigger('custom-ready')  

my original answer (for context):

i've had appeared same issue in past different setup, in case loading //www.youtube.com/iframe_api.

in scenario able move code relied on youtube player api being ready window.onyoutubeplayerapiready callback , used standard <script> include after, example:

... script tags non-youtube reliant code ... <script src="...jquery, plugins, site js..."></script>  ... youtube specific code followed youtube api script tag... <script>     window.onyoutubeiframeapiready = function () {         $('.video-gallery').gallery();     } </script> <script src="//www.youtube.com/iframe_api"></script> 

however, believe option trigger custom event ready state, example (in coffeescript):

$(document).bind 'custom-ready', () ->      ... code in ready event callback ...  $ ->      # check if page includes youtube player api (this     # achieved in number of ways, here assume css class      # applied page body indicator.     if $('body.uses-youtube-player-api').length == 0          # page doesn't use youtube player api call         # custom ready event now.         $(document).trigger('custom-ready')  # if page load youtube player api trigger our custom # ready event in associated callback. window.onyoutubeplayerapiready = () ->     $(document).trigger('custom-ready') 

i haven't tested above example , it's suggestion since imagine original example of how i've resolved issue isn't ideal in many cases.

remembering believe issue appeared caused when youtube player api code cached browser. prove case use random string load api player (e.g prefixing ?_ignore={insert random value} script tags src attribute) , see if resolves issue, if wouldn't efficient solution user require library reloaded every visit.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -