Can events fired from an iframe be handled by elements in its parent?
Clash Royale CLAN TAG#URR8PPP
Can events fired from an iframe be handled by elements in its parent?
Suppose I have a page located at www.example.com/foo
, and it contains an <iframe>
with src="http://www.example.com/bar"
. I want to be able to fire an event from /bar
and have it be heard by /foo
. Using the Prototype library, I've tried doing the following without success:
www.example.com/foo
<iframe>
src="http://www.example.com/bar"
/bar
/foo
Element.fire(parent, 'ns:frob');
When I do this, in Firefox 3.5, I get the following error:
Node cannot be used in a document other than the one in which it was created" code: "4
Line 0
Not sure if that's related to my problem. Is there some security mechanism that's preventing scripts in /bar
from kicking off events in /foo
?
/bar
/foo
@Annie the scheme and domain portions of the URL's are the same.
– allyourcode
Jan 13 '10 at 21:16
4 Answers
4
I haven't tested this cross-browser yet, but it works in FF.
In the iFrame you can fire on the element parent.document:
Event.observe(window, 'load', function()
parent.document.fire('custom:event');
);
and in the parent frame you can catch it with:
document.observe('custom:event', function(event) alert('gotcha'); );
Ah. I think the key here is that you used parent.document. The difference is that parent is a Window object vs. parent.document is an Element. Thanks!
– allyourcode
Apr 7 '10 at 5:00
Events can be handled by a function defined the parent window if the iframe
is a page from the same domain (see MDC's article on Same Origin Policy); however, events will not bubble up from the iframe
to the parent page (at least not in my tests).
iframe
iframe
Thanks alot, Justin. Can you (or someone else) provide a link about events not bubbling up from an iframe?
– allyourcode
Jan 13 '10 at 21:22
From the specs: "propagation will continue up to and including the Document." It is important to understand that an iframe has an own document element. Again, citing the specs: "the embedded document remains independent of the main document"
– user123444555621
Aug 25 '10 at 19:29
Specifications are rarely the same as implementations :(
– Justin Johnson
Aug 25 '10 at 23:58
This is unfortunate, but I understand the reason. It just means whomever is in control of the other domain should provide hooks to pass events through (assuming you're using a 3rd party library like me).
– kamranicus
Aug 29 '13 at 15:18
rather then catch an event on the main page, you can catch the event at the iframe, and call a function on the main page.
<-- main page -->
function catchIt()
// code here
<-- iframe page -->
function doIt()
top.catchIt();
<a onclick="doIt();">test</a>
i think it would work but didnt test it
Don't you mean parent.catchIt, not top.catchIt?
– allyourcode
Jan 13 '10 at 21:19
when you have frames inside other frames, top means the parent os all frames.
– TeKapa
Jan 14 '10 at 12:51
This should work as expected, to do what you need:
index.html:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(function()
$(document).on('eventhandler', function()
alert('event was fired');
);
);
</script>
iframe.html:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(function()
parent.$(parent.document).trigger('eventhandler');
);
</script>
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Are foo and bar in the same domain?
– Annie
Jan 12 '10 at 4:34