0
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Youtube embed test</title>
<script type="text/javascript" src="swfobject/swfobject.js"></script>
<script type="text/javascript">
function onYouTubePlayerReady(playerid)
{
ytplayer = document.getElementById("myytplayer");
}
function embedPlayer()
{
var params = { allowScriptAccess: "always" };
var atts = { id: "myytplayer" };
swfobject.embedSWF("http://www.youtube.com/apiplayer?enablejsapi=1",
"ytapiplayer", "1024", "768", "8", null, null, params, atts);
}
function loadVideo(id, start)
{
ytplayer.loadVideoById(id, start);
}
</script>
</head>
<body onload="embedPlayer()">
<div id="ytapiplayer">
You need Flash player 8+ and JavaScript enabled to view this video.
</div>
<a href="javascript:void(0);" onclick="loadVideo('bBObeZ6Vj3A',2);">Load</a>
</body>
</html>
我是Javascript新手,因此無法使用此工具。調試器告訴我,函數loadVideo()中未定義ytplayer。爲什麼是這樣?我知道在大括號結束時,onYouTubePlayerReady中聲明的變量超出了範圍。那麼我怎樣才能訪問loadVideoById方法?我進一步困惑的是,我可以這樣做:Javascript範圍
<a href="javascript:void(0);" onclick="ytplayer.loadVideoById('e1h5TzdTq0o', 0);">test</a>
事實證明,這是完全合法的。但爲什麼我允許使用ytplayer?我假設它與YouTubePlayerReady上的回調函數有關?