Restart youtube api

I need to upload, destroy and reload the YouTube Upload Widget , but unfortunately the reboot does not work.

I have completed the following steps:

  • I downloaded Youtube Upload Widget as described in the instructions
  • I am shooting a video from a webcam - it works
  • I destroy the widget with widget.destroy( ) - it works
  • I remove the script element from HTML - it works
  • I reload the API as in step 1 - nothing happens

Why is this not working?

+4
source share
2 answers

onYouTubeIframeAPIReady() will only be called once per page when the window.YT.* interface is loaded through an external script. Removing the <script src="http://www.youtube.com/iframe_api"> element from the page and then re-adding it will not onYouTubeIframeAPIReady() again.

If you want to destroy the <iframe> that contains the download widget, then create a new widget placed in the new <div> that should work, but you shouldn't do it inside the onYouTubeIframeAPIReady() callback a second time. window.YT.UploadWidget() already available in this place, so feel free to use this interface directly from anywhere in your code.

+3
source

onYouTubeIframeAPIReady () is called only once when the YouTube API loads. The answer is to add another check to step 2 of YouTube:

 if (typeof tag === "undefined") { // if first run load the YT API which will call the correct functions. var tag = document.createElement('script'); tag.src = "https://www.youtube.com/iframe_api"; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); } else { // if YT api already loaded, we need to call the function. onYouTubeIframeAPIReady(); } 

Good luck.

+2
source

Source: https://habr.com/ru/post/1442392/


All Articles