【发布时间】:2015-05-27 15:30:52
【问题描述】:
我在域 A 有一个网站,其中包含一个用于显示视频的 iframe,该视频托管在 YouTube 上。在 iframe 加载事件中,我试图在 iframe 中注册 keyup 事件,以便在客户端按下退出键时关闭视频。这是我的代码示例:
$(myIFrame).bind('load', function() {
$(myIFrame.contentWindow.document).keyup(function(event) {
console.log(event.keyCode);
});
alert('Event Registered');
});
我遇到以下异常:
火狐:Error: Permission denied to access property "document"
铬:Uncaught SecurityError: Blocked a frame with origin "A" from accessing a frame with origin "https://www.youtube-nocookie.com". Protocols, domains, and ports must match.
有没有办法在跨域 iframe 中注册事件?
【问题讨论】:
-
你能从域B更改页面的源代码吗?
-
不,很遗憾,我完全无法控制域 B。B 是 youtube-nocookie.com。
-
因此出于安全原因,您不能使用 javascript。
标签: javascript jquery security youtube