2011-08-11 167 views
5

很少有這方面的主題,但沒有答案給我。嵌入式YouTube視頻並不停止在Chrome瀏覽器和IE瀏覽器

嵌入YouTube視頻在覆蓋DIV:

<div id="blanket" style="display:none;"> 
    </div> 
     <div id="popUpDiv" style="display:none;"> 
     <a href="#" onclick="popup('popUpDiv')">Close</a>  
     <iframe width="480" height="390" src="https://www.youtube.com/embed/G5AuItKv?rel=0&autoplay=1" frameborder="0" allowfullscreen></iframe>          
     </div> 

在Firefox當我關閉視頻停止窗口,但無法在Chrome或Internet Explorer。

是否有一些簡單的JavaScript我可以用它來阻止它,當用戶點擊關閉?

TIA

編輯:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 

<body> 

<a href="#" onclick="javascript:ytplayer.stop()">Close</a> 

<iframe width="480" height="390" src="http://www.youtube.com/e/PE1il5znICA?enablejsapi=1&version=3&playerapiid=ytplayer" frameborder="0" allowfullscreen></iframe> 


<script type="text/javascript"> 
function onYouTubePlayerReady(playerId) { 
    ytplayer = document.getElementById("ytplayer"); 
} 

function stop() { 
    if (ytplayer) { 
    ytplayer.stopVideo(); 
    } 
} 
</script> 
</body> 
</html> 

回答

3

YouTube有一種JavaScript API,可以啓用,以停止視頻。要啓用api,請將此參數:&enablejsapi=1添加到您的src網址的末尾,現在您可以通過javascript訪問播放器。首先,你需要得到玩家參考:

function onYouTubePlayerReady(playerId) { 
    ytplayer = document.getElementById("nameofplayer"); 
} 

,現在你必須停止用下面的函數視頻的能力:

function stop() { 
    if (ytplayer) { 
    ytplayer.stopVideo(); 
    } 
} 

只需打個電話給stop();近距離的onclick函數內。

所有youtube javascript api信息位於here

+0

進一步檢查iframe youtube api稍有不同。請參閱[這裏](https://code.google.com/apis/youtube/iframe_api_reference.html#Loading_a_Video_Player)以瞭解更多關於如何通過javascript訪問播放器的信息。 –

1

我在HTML5中遇到同樣的問題。 Iframe不適用於Youtube API,您必須通過啓用JavaScript API來生成對象。

嘗試改變:

<iframe width="480" height="390" src="http://www.youtube.com/e/PE1il5znICA?enablejsapi=1&version=3&playerapiid=ytplayer" frameborder="0" allowfullscreen></iframe> 

到:

swfobject.embedSWF("http://www.youtube.com/e/PE1il5znICA?enablejsapi=1&version=3&playerapiid=ytplayer","ytplayer", "480", "390", "8", null, null, params, atts); 

例:

首先添加的div HTML:

<div id="ytplayer"> 
<p>You need Flash player and JavaScript enabled to view this video.</p> 
</div> 

然後添加腳本來創建對象和添加停止功能:

<script type="text/javascript"> 

    function onYouTubePlayerReady(playerId){ytplayer = document.getElementById("ytplayer");} 
     var params = { allowScriptAccess: "always" };var atts = { id: "ytplayer" }; 


     swfobject.embedSWF("http://www.youtube.com/e/videoID?enablejsapi=1&version=3&playerapiid=ytplayer","ytplayer", "425", "356", "8", null, null, params, atts); 

     function stop() {if (ytplayer) {ytplayer.stopVideo();}} 
    </script> 

適合我。

相關問題