JavaScript method to track YouTube video completion status

I’m building a website that contains many YouTube videos embedded throughout different pages. My client has requested that we display a modal dialog box every time a video reaches its end.

After checking the YouTube JavaScript API documentation, I found there are event listeners available for tracking video completion. However, the challenge is that all our videos were already embedded using the standard iframe embed code that users copied directly from YouTube.

We have hundreds of these videos already live on the site, and manually updating each embed would be extremely time consuming. Is there a JavaScript solution that can monitor when these existing embedded videos finish playing without requiring us to modify the current embed codes?

I need a way to attach event handlers to detect the end state of videos that are already embedded using the default YouTube iframe method.

for sure! you can utilize the postMessage API to track youtube events even with the iframes you already have. just append ?enablejsapi=1 to your iframe src and then listen for ‘message’ events on the window. youtube will send a message when the video finishes, allowing you to show your modal.

Been dealing with similar challenges on enterprise video platforms. The key thing to remember is that YouTube’s iframe API requires enabling the JavaScript API parameter, but rather than modifying URLs directly, consider using a more robust approach. You can create a wrapper function that intercepts the iframe loading process and handles the API initialization automatically. This involves scanning the DOM for existing YouTube embeds and creating new YT.Player instances programmatically while preserving the original video IDs. The advantage is better error handling and more reliable event detection compared to relying solely on postMessage events. Just ensure you load the YouTube iframe API script before attempting to initialize players, and implement proper fallback mechanisms for videos that might fail to load properly.

Actually ran into this exact scenario about six months ago when working on a learning platform. The postMessage approach mentioned above works well, but you’ll need to handle the iframe modifications programmatically rather than manually updating hundreds of embeds. What I did was write a script that runs on page load to find all YouTube iframes and automatically append the enablejsapi parameter if it’s missing. Something like iframe.src += '&enablejsapi=1' works for most cases. Then set up a global message listener that checks for the video state change events from YouTube’s API. The tricky part is that you need to be careful about cross-origin restrictions and make sure your domain is whitelisted if you’re running this in production. Also worth noting that some older embedded videos might have slightly different URL formats that could break the parameter appending logic, so test thoroughly with your existing content first.