Firing events between an iframe and its parent using YUI
I have an iFrame that is firing an event that I want the parent page to pick up. Essentially the inver开发者_运维问答se of this question. I can pick up the event inside the iFrame, so I know it's firing, but nothing happens in the parent.
I'm using YUI 3 so any answers based around this get a double-thumbs up, but all help is gratefully received.
Apparently events don't propagate across frames. Here's how I'm working around it:
In the parent frame define this global function:
var fireGlobalEvent = function (e, param) {
YUI().use('event-custom', function (Y) {
var publisher = new Y.EventTarget();
publisher.publish(e, {
broadcast: 2, // global notification
emitFacade: true // emit a facade so we get the event target
}).fire(param);
});
};
Then just call this in the child to trigger the event in the parent.
window.parent.fireGlobalEvent('my_custom_global_event', 'an_extra_param');
Then the event is caught by parent modules with:
Y.Global.on('my_custom_global_event', function (e, param) {
// do something
});
精彩评论