简体   繁体   中英

Passing events from iframe to parent

I try to load an iframe which inside I trigger my custom events, those events need to be captured in a parent DOM and activate functions there.

I tried to look for different approaches but none worked, I know I can call parent.postMessage(), but the issue is I dont know how many parents (generations) I have.

I would like to just call postMessage(), and it will propogate to all parents and captured on those who listen to the event.

Is this possible at all?

Been a while since I've had to do anything like this, but I think you'd need to control this the other way around, so trigger the events at window scope within the iframe, and handle the events within the parent frame.

//Inside iFrame
$(window).trigger('myevent', { a: 'b' });

//In parent document
$('#myIframe').on('myevent', function(e) {
    //Handling the event.
});

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