Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <div id="popupVid" style="width:500px;height:auto;">
   <iframe width="500" height="315" 
   src="http://www.youtube.com/embed/y9QEQHe8ax4?enablejsapi=1&autoplay=1" 
   frameborder="0" allowfullscreen></iframe>
</body>
</html>
 
function toggleVideo(state) {
    // if state == 'pause', pause. Else: play video
    var iframe = document.querySelector("iframe").contentWindow;
    
    if (state === 'pause') {
      document.title = '|| paused.';
      iframe.postMessage('{"event":"command","func":"pauseVideo","args":""}','*');
      document.body.style.background = "lightgray";
    } else {
      document.title = '> playing...';
      iframe.postMessage('{"event":"command","func":"playVideo","args":""}','*');
      document.body.style.background = "lightgreen";
    }
}
var focus = false;
window.onblur = function() { focus=false; toggleVideo('pause'); }
window.onfocus = function(){ focus=true; toggleVideo(); }
document.onblur = window.onblur;
document.focus = window.focus;
var vis = (function(){
    var stateKey, eventKey, keys = {
        hidden: "visibilitychange",
        webkitHidden: "webkitvisibilitychange",
        mozHidden: "mozvisibilitychange",
        msHidden: "msvisibilitychange"
    };
    for (stateKey in keys) {
        if (stateKey in document) {
            eventKey = keys[stateKey];
            break;
        }
    }
    return function(c) {
        if (c) document.addEventListener(eventKey, c);
        return !document[stateKey];
    }
})();
vis(function(){
  if (vis()) toggleVideo();
  else toggleVideo('pause');
});
setTimeout(function() { if(focus) toggleVideo(); else toggleVideo('pause'); }, 1000);
Output 300px

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers