简体   繁体   English

无法监听来自其他域的iframe中的事件

[英]Cant listen to events from iframe with a different domain

I created a page ( http://slots.trioland.com/slots/ironman/ ) that includes an iframe with src on a different domain, which after a minute or 2 redirects me to another page. 我创建了一个页面( http://slots.trioland.com/slots/ironman/ ),其中包括一个在不同域上带有src的iframe,在一两分钟后将其重定向到另一个页面。 I am trying to avoid the redirection by listening to the event messages from the source inside the iframe: 我试图通过侦听iframe内源中的事件消息来避免重定向:

postMessage事件

Therefore,I created the following code to catch the message: 因此,我创建了以下代码来捕获消息:

function slotslibrequest(event){
  if (event.origin !== "http://slotslib.com")
    return;
  event.source.postMessage($('.flash.flash-inner').width(),
                           event.origin);
}

if (window.addEventListener) {
 // For standards-compliant web browsers
window.addEventListener("message", slotslibrequest, false);
}
else {
 window.attachEvent("onmessage", slotslibrequest);
}

The problem is that the listener that I created is not being able to listen to the postMessage from the iframe. 问题是我创建的侦听器无法侦听iframe中的postMessage。 Anyone knows where am I wrong? 有人知道我在哪里错吗?

Thanks in advance, 提前致谢,

Is this possible? 这可能吗?

I am regretful to say "No". 我很遗憾地说“不”。 iframes are considered (by the browser engine) as a different DOM document. iframe被浏览器引擎视为另一个DOM文档。 You need to manage this in the inner iframe application. 您需要在内部iframe应用程序中进行管理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM