1 var tag = document.createElement('script'); 2 tag.src = "https://www.youtube.com/iframe_api"; 3 var firstScriptTag = document.getElementsByTagName('script')[0]; 4 firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); 5 6 var player; 7 function onYouTubeIframeAPIReady() { 8 player = new YT.Player('player', { 9 height: '390', 10 '640', 11 videoId: 'M7lc1UVf-VE', 12 playerVars: { 13 hl: 'ja_JP', 14 loop:0, 15 vq:'large',//有时拖拽时, 总是在加载, 但不能播放(IE) 16 rel: 0, 17 '*origin': '', 18 wmode:'transparent' 19 }, 20 events: { 21 'onReady': onPlayerReady, 22 'onStateChange': onPlayerStateChange 23 } 24 }); 25 } 26 27 function onPlayerReady(event) { 28 event.target.playVideo(); 29 } 30 31 //有时点击重新播放, 视频无反应, 处理(IE) 32 function onPlayerStateChange(event) { 33 if (event.data===YT.PlayerState.ENDED) { 34 event.target.cueVideoById(event.target.getVideoData().video_id); 35 } 36 } 37 38 //如果是弹出层显示的YouTube视频, 在关闭时的处理 39 if(player.pauseVideo){ 40 player.pauseVideo();//html5 41 } else{ 42 player._stop = true;//flash(IE9等) 43 }