我有这个youtube代码:
< iframe width =“970”height =“582”src =“来自YOUTUBE的嵌入代码”frameborder =“0”allowfullscreen>< / iframe>
现在,我想在用户点击视频时(当他点击播放时)执行某些操作。 我试图获取iframe点击,嵌入点击..没有任何作用。我做错了什么?
谢谢!
答案 0 :(得分:2)
您不能在iframe
内执行任何操作,这不是来自同一网站。这是禁止的。阅读same origin policy。
答案 1 :(得分:1)
我刚刚在Google Code游戏中写了这个修改:
<!--
You are free to copy and use this sample in accordance with the terms of the
Apache license (http://www.apache.org/licenses/LICENSE-2.0.html)
-->
<!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>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>YouTube Player API Sample</title>
<style type="text/css">
#videoDiv {
margin-right: 3px;
}
#videoInfo {
margin-left: 3px;
}
</style>
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">
google.load("swfobject", "2.1");
</script>
<script type="text/javascript">
/*
* Polling the player for information
*/
// This function is called when the player changes state
function onPlayerStateChange(newState) {
if(newState == 3)
alert('Video playing');
}
// This function is automatically called by the player once it loads
function onYouTubePlayerReady(playerId) {
document.getElementById("ytPlayer").addEventListener("onStateChange", "onPlayerStateChange");
}
// The "main method" of this sample. Called when someone clicks "Run".
function loadPlayer() {
// The video to load
var videoID = "ylLzyHk54Z0"
// Lets Flash from another domain call JavaScript
var params = { allowScriptAccess: "always" };
// The element id of the Flash embed
var atts = { id: "ytPlayer" };
// All of the magic handled by SWFObject (http://code.google.com/p/swfobject/)
swfobject.embedSWF("http://www.youtube.com/v/" + videoID +
"&enablejsapi=1&playerapiid=player1",
"videoDiv", "480", "295", "8", null, null, params, atts);
}
function _run() {
loadPlayer();
}
google.setOnLoadCallback(_run);
</script>
</head>
<body style="font-family: Arial;border: 0 none;">
<div id="videoDiv">Loading...</div>
</body>
</html>
如果您使用这种方式嵌入视频,则只要视频状态发生变化,就会调用onPlayerStateChange函数。 state == 3表示视频刚开始播放。 state == 1表示视频正在播放,如果你想多次检测(IE如果它们暂停并再次播放)你需要使用newState == 1我把newState == 3.但是如果你只想检测他们第一次启动视频时应该有效。