简体   繁体   中英

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. I am trying to avoid the redirection by listening to the event messages from the source inside the 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. 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. You need to manage this in the inner iframe application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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