Triggering events between an iframe and its parent using YUI

I have an iFrame that fires an event that I want the parent page to pick up. Essentially the opposite of this question . I can pick up the event inside the iFrame, so I know that it fires, but nothing happens in the parent.

I use YUI 3, so any answers based on this get double thumbs up, but any help is greatly appreciated.

thanks

Rob

+4
source share
1 answer

Events do not seem to propagate through frames. Here's how I work on 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 captured by the parent modules with:

  Y.Global.on('my_custom_global_event', function (e, param) { // do something }); 
+4
source

All Articles