Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9222495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:50:47+00:00 2026-06-18T03:50:47+00:00

YouTube’s iFrame embed JS player API allows you to add callbacks to certain events.

  • 0

YouTube’s iFrame embed JS player API allows you to add callbacks to certain events. I want to add a callback for when a related video at the end of a video is selected.

To be more specific, when viewing a video in an embed, at the end it displays related videos within the embed. I want to run some code when one of those is selected. How can this be accomplished? I see that there is an onStateChange but none of the states are related to related videos. Do I need to add an onStateChange for YT.PlayerState.PLAYING and then compare the playing video to the original video to see if they’re different somehow?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T03:50:48+00:00Added an answer on June 18, 2026 at 3:50 am

    I ended up doing it basically the way I described. Here’s the code I used:

    started = false;
    
    var onYouTubeIframeAPIReady = function(id) {
      var player = new YT.Player('player', {
        videoId: id,
        events: {
          'onStateChange': function (event) {
            if (event.data == 1) { // The video started playing
              started = true;
            }
            if (started && event.data == -1) {
              // Video had already started playing before and is now in
              // "unstarted" state so it must be a new video.
              var video_url = event.target.i.videoUrl;
              var video_id = video_url.replace('http://www.youtube.com/watch?v=', '').replace('&feature=player_embedded', '');
              window.location = '#/view/' + video_id;
            }
          }
        }
      });
    }
    

    So basically, when a video starts playing, you set a “started” variable to true. Then, if/when the video enters the “unstarted” state, if “started” is true, then you know that it’s a new video that just started playing. In that case, grab its video ID from the event.target object and do whatever you want with it.

    My full commit is here if anyone wants to see the context and you can see it in action on http://toogl.es.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to add youtube video list but not video player. So what I
YouTube allows to use <iframe> to embed videos on sites in addition to flash
YouTube iframe API: how do I control a iframe player that's already in the
According to the Google Youtube Java API if I want to delete a video,
Using YouTube's JavaScript API to control an iFrame embed. I've set up the functions
I have a youtube video that I want to be by default HD (720),
I want to play youtube video in windows 8 application with XAML and c#.
I am using the Youtube api for asp.net to rename a video: public static
The youtube embed code's general structure is like this: <iframe width=560 height=349 src=http://www.youtube.com/embed/MiYND_zvIc0 frameborder=0
In video gallerys YouTube shows images of the videos instead of the flash player.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.