Is it possible to set an onLoad event listener on an IFrameElement?

452 views
Skip to first unread message

Jake

unread,
Jul 17, 2009, 1:56:37 AM7/17/09
to Google Web Toolkit
Hi all,

I just have a quick question: I know that GWT presents a fairly low-
level DOM API that wraps native DOM objects. What I'd like to do is
create a new iframe element, and then set a DOM onLoad listener on it
so that I can then manipulate the iframe's internal document (its
contentDocument). It's very easy for me to imagine how to implement
this, except for the fact that I cannot find a way to set any kind of
a listener on any of the DOM objects exposed by GWT. I found some blog
posts that mentioned that GWT's method of event handling had changed
in 1.6 [0][1], but I'm not sure if that applies only to classes that
extend Widget, or to the wrapped DOM objects as well. Hopefully I'm
just missing them.

I'd greatly appreciate any guidance anyone can offer. Thanks,

Jake

[0] http://lemnik.wordpress.com/2009/03/04/gwts-new-event-model-handlers-in-gwt-16/
[1] http://lemnik.wordpress.com/2009/03/12/using-event-handlers-in-gwt-16/

Jason Morris

unread,
Jul 17, 2009, 4:39:54 AM7/17/09
to Google-We...@googlegroups.com
Hi Jake,

Unfortunately the DOM structure is bound more to the JavaScript way of doing things than Widgets. In
JavaScript you can't have more than one event listener (of a given event type) per element, while
Widgets may have any number.

So to work around this: GWT Widgets are the only listeners on their nested Element object (notice
they implement the com.google.gwt.user.client.EventListener interface). SO when they receive an
event from the browser, they decode it into a DOMEvent and dispatch it to all the Handlers
registered for that Widget.

Unfortunately there is no simple way to deal with this on the DOM nodes themselves, and in
particular there is no simple way to deal with IFrame loading events. If you take a look at how
FormPanel receives loading events from it's hidden IFrame there are two different implementations
(one for IE6, and the generic implementation).

For normal browsers(com.google.gwt.user.client.ui.impl.FormPanelImplIE6), the event is hooked using:
iframe.onload = function() {
IE6 (com.google.gwt.user.client.ui.impl.FormPanelImplIE6) looks like:
iframe.onreadystatechange = function() {
if (iframe.readyState == 'complete') {

Both implementations also ensure that they "unhook" the events, by assigning the listener function
to null (see FormPanel.onAttach / onDetach). I would consider extending the Frame Widget rather than
using the DOM api directly. Widgets provide loads of useful safety nets that you otherwise need to
write yourself (including attaching multiple Handlers to the element).

Hope this Helps.
//J
Reply all
Reply to author
Forward
0 new messages