Iframe / Javascript Check if Youtube Video Ends

后端 未结 3 2113
北恋
北恋 2020-12-18 11:22

Right now I have youtube videos loading in via Iframe. Is it even possible to check if the youtube video ended when it is in that sort of state? Can I use OnStateChange() t

相关标签:
3条回答
  • 2020-12-18 11:40

    you can detect the end of the video like so:

    function onPlayerStateChange(event) {
      if (event.data == YT.PlayerState.ENDED){
        // your code to run after video has ended
      }
    }
    
    0 讨论(0)
  • 2020-12-18 11:42

    If you have an iframe on your page, and its src attribute contains a URL which does not have the same domain, protocol or port as the container page, you can not access its document because of Same Origin Policy.

    You may want to look into YouTube supporting postMessage().

    If you do own the iframe's contents, then get its iframe.contentDocument where you can access the iframe's document.

    0 讨论(0)
  • 2020-12-18 12:04

    Yes, but it's an experimental API:

    https://developers.google.com/youtube/iframe_api_reference

    0 讨论(0)
提交回复
热议问题