Youtube hidden player loses its methods

I control a built-in player without youtube headers with javascript, and I want to hide it sometimes by setting the display to: none. However, when I show the player again, he loses his youtube methods.

For example:

<script>
  swfobject.embedSWF("http://www.youtube.com/apiplayer?enablejsapi=1&playerapiid=player",
    "player", "425", "356", "8", null, null, 
    {allowScriptAccess: "always"}, {id: 'player'}
  );

  var player = null;

  function onYouTubePlayerReady(playerId) {
    player = document.getElementById(playerId);
    player.addEventListener('onStateChange', 'playerStateChanged');
  }

  function hidePlayer() {
    player.pauseVideo();
    player.style.display = 'none';
  }
  function showPlayer() {
    player.style.display = 'block';
    player.playVideo();
  }
</script>
<a href="#" onClick="hidePlayer();">hide</a>
<a href="#" onClick="showPlayer();">show</a>
<div id="player"></div>

A call to hidePlayer followed by showPlayer throws this error when calling playVideo:

Uncaught TypeError: Object #<an HTMLObjectElement> has no method 'playVideo'

The only solution I can find is to use visibility: hidden, but it messes with my page layout. Any other solutions out there?

+5
source share
2 answers

It happened to me in my zip code too. You can try just making it 1x1 px and not hide it at all.

, , .

0

: : , CSS, div : .

HTML:

<div id="ytwrapper">
    <div id="ytplayer">
    </div>
</div>

CSS

#ytwrapper { overflow: hidden; }

JS:

function hidePlayer() {
    player.pauseVideo();
    player.style.marginLeft = "50000px";
}

function showPlayer() {
    player.style.marginLeft = "0px";
    player.playVideo();
}
0

All Articles