Iframe / Javascript Check if Youtube video is complete

Right now I have a YouTube video upload using an iframe. Is it even possible to check if a YouTube video has ended when it is in this state? Can I use OnStateChange () through an iframe?


source to share

3 answers

Yes, but this is an experimental API:




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




If your page has iframe

, and its attribute src

contains a URL that does not have the same domain, protocol, or port as the container page, you cannot access it document

due to the Same Origin Policy .

You can watch on YouTube by supporting postMessage()


If you own the content iframe

, then get it iframe.contentDocument

where you can access it iframe





All Articles