title: Detecting when a youtube video finishes
body: <p>I know there are several questions for this topic. I even got a great answer <a href="https://stackoverflow.com/questions/7853904/how-to-detect-when-a-youtube-video-finishes-playing">here</a>.</p>
  
  <p>The problem is the following. I have a piece of code I can't change that shows the youtube video. I have to adapt to it.</p>
  
  <p>The problem is that the function onPlayerStateChange() never start. Any idea why this happens?</p>
  
  <pre><code>&lt;script src="http://www.youtube.com/player_api"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
  var ytPlayer;
  var idplayer;
  function onYouTubePlayerReady(playerId) {
      ytPlayer = document.getElementById("ytPlayer");
  }
  
  function pauseVideo() {
    if (ytPlayer) {
      ytPlayer.stopVideo();
    }  
  }
  
  // when video ends
  function onPlayerStateChange(event) {        
      if(event.data === 0) { 
      idplayer=document.getElementById("a_TMBLRWDLH").href;
          window.location.assign(idplayer);
      }
  }
  
  j$(document).ready(function(){
  
  
      j$('#' + Bnr_vars[0].banner).click(function(){
  
          if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i)))
  {
      var info='&lt;iframe width=\"600\" height=\"338\" src=\"http://www.youtube.com/embed/0Bmhjf0rKe8?rel=0&amp;wmode=transparent&amp;modestbranding=1&amp;autoplay=1&amp;enablejsapi=1\" frameborder=\"0\" allowfullscreen&gt;&lt;/iframe&gt;';
      j$('#player').html(info);
  }
  
  });
  &lt;/script&gt;
  </code></pre>