CoffeeScript - 等效的 JS 代码无法播放嵌入式 Youtube 视频
我正在使用 swfObject 在网站上嵌入 Youtube 视频,以便我可以使用 Youtube Player API 对其进行操作。然而,我正在用 Coffeescript 重写该网站,并陷入了这个问题。
这是 html:
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript" src="js/swfobject.js">//embedding youtube videos</script>
<script type="text/javascript" src="coffee/test.js"></script>
</head>
<body>
<div id="ytplayer">
<p>You will need Flash 8 or better to view this content.</p>
</div>
</body>
<script type="text/javascript">
var params = { allowScriptAccess: "always" };
swfobject.embedSWF( "http://www.youtube.com/v/yeClJneSNXA&enablejsapi=1&playerapiid=ytplayer", "ytplayer", "425", "365", "8", null, null, params);
</script>
</html>
以下代码可以工作:
ytplayer = document.getElementById('ytplayer');
ytplayer.playVideo();
但是,等效的(我认为)coffeescript 代码不能:
ytplayer = document.getElementById("ytplayer")
ytplayer.playVideo()
它编译为以下 JS:
(function() {
var ytplayer;
ytplayer = document.getElementById("ytplayer");
ytplayer.playVideo();
}).call(this);
它在 Firebug 中给了我以下错误:
ytplayer.playVideo 不是函数
返回 ytplayer.playVideo();
I am embedding a Youtube video on a website using swfObject so I can manipulate it using Youtube Player API. However, I am in the process of rewriting the site in Coffeescript and am stuck on this problem.
Here is the html:
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript" src="js/swfobject.js">//embedding youtube videos</script>
<script type="text/javascript" src="coffee/test.js"></script>
</head>
<body>
<div id="ytplayer">
<p>You will need Flash 8 or better to view this content.</p>
</div>
</body>
<script type="text/javascript">
var params = { allowScriptAccess: "always" };
swfobject.embedSWF( "http://www.youtube.com/v/yeClJneSNXA&enablejsapi=1&playerapiid=ytplayer", "ytplayer", "425", "365", "8", null, null, params);
</script>
</html>
The following code works:
ytplayer = document.getElementById('ytplayer');
ytplayer.playVideo();
However, the equivalent (I think) coffeescript code DOES NOT:
ytplayer = document.getElementById("ytplayer")
ytplayer.playVideo()
Which compiles to the following JS:
(function() {
var ytplayer;
ytplayer = document.getElementById("ytplayer");
ytplayer.playVideo();
}).call(this);
It gives me the following error in Firebug:
ytplayer.playVideo is not a function
return ytplayer.playVideo();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这很可能是因为在您尝试播放 YouTube 视频之前 YouTube 播放器尚未完成加载。
试试这个:
这会将您的代码放入 YouTube 用于执行操作的 onYouTubePlayerReady 回调中让您知道玩家已准备就绪。
我假设 JS 代码有效,因为您在 Firebug 中运行了它?如果是这样,那就有意义了,因为当您在控制台中输入该命令时,YouTube 播放器很可能会被加载。
This is most likely because the YouTube player hasn't finished loading before you try to play the YouTube video.
Try this:
This puts your code in the onYouTubePlayerReady callback that YouTube uses to let you know the player is ready.
I'm assuming the JS code worked because you ran it inside of Firebug? If so, that would make sense because by the time you enter that command into the console, the YouTube player will most likely be loaded.