javascript 如何在 iFrame 中捕获嵌入式 YouTube 视频的点击?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11893171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:41:29  来源:igfitidea点击:

How to capture onclick within an iFrame for embedded YouTube video?

javascripthtmliframe

提问by genxgeek

I have an iFrame that displays an embedded youtube video.

我有一个显示嵌入式 YouTube 视频的 iFrame。

<iframe width="560" height="315" src="http://www.youtube.com/embed/xxx" frameborder="0" allowfullscreen></iframe>

What I would like to do is somehow capture the click event when the user clicks the you tube video so that I can call an http://myserver.com/dostuffrest api to update an external server counter tracking the number of clicks and of course allow the video to play as expected.

我想做的是在用户单击 You Tube 视频时以某种方式捕获单击事件,以便我可以调用http://myserver.com/dostuffrest api 来更新跟踪点击次数和点击次数的外部服务器计数器当然允许视频按预期播放。

Any information would be greatly appreciated.

任何信息将不胜感激。

采纳答案by genxgeek

Everybody had great feedback. Thanks for all who posted. After trying some hacks the bottom line is that trapping the click event is not natively supported via html or javascript.

每个人都有很好的反馈。感谢所有发帖的人。在尝试了一些技巧之后,底线是通过 html 或 javascript 本身不支持捕获点击事件。

The best artifact and cleanest solution that I have found is here.

我找到的最好的神器和最干净的解决方案是这里

回答by 404 Not Found

You can't, I believe. That would be a security risk. If you mainly want to do tracking and whatnot, you would probably be best off doing something like adding an image/button in the place of the video that the user clicks, causing the video to appear.

你不能,我相信。那将是一个安全风险。如果您主要想进行跟踪等等,您可能最好做一些事情,例如在用户单击的视频的位置添加图像/按钮,从而导致视频出现。

Edit: This solution may work too; I'm not sure if it'll work for iframes that aren't on the same domain.

编辑:此解决方案也可能有效;我不确定它是否适用于不在同一域中的 iframe。

回答by Eugene Naydenov

window.postMessagecould help. But you should have an access to source code of the page you're rendering in iframe. So in case of embedding of youtube video, you probably can't deal with this.

window.postMessage可以提供帮助。但是您应该可以访问在 iframe 中呈现的页面的源代码。因此,在嵌入 youtube 视频的情况下,您可能无法处理。