How to get readyState from GWT Frame object?

1,490 views
Skip to first unread message

elloalisboa

unread,
Jul 21, 2011, 2:28:51 PM7/21/11
to Google Web Toolkit
I'm using GWT 2.3.0 in my project and I have an iframe and a loading
mask. When the iframe content (to be more specific, the content is a
PDF) is loaded I need to hide the loading mask.

I tried using:

Frame frame = new Frame("http://www.example.com/example.pdf") {{
addDomHandler(new LoadHandler() {
public void onLoad(LoadEvent event) {
loadingMask.setVisible(false);
}
}, LoadEvent.getType());
}};

and:

Frame frame = new Frame("http://www.example.com/example.pdf") {
@Override
protected void onLoad() {
super.onLoad();
loadingMask.setVisible(false);
}
};

Because of the content is a PDF, both codes don't work in IE.

Is there a way to get the readyState iframe property with GWT?

I would appreciate any feedback. Thanks for all.

jhulford

unread,
Jul 22, 2011, 12:24:20 PM7/22/11
to Google Web Toolkit
I had the same issue and ended up having to write some JSNI to query
the frame's readyState every X milliseconds for IE (load event wasn't
firing consistently). It's pretty straightforward to do, but I don't
have the code any more because there were just too many other IE
issues with the iframe download approach the way I had been doing it
that I just used the simple anchor tag downloads instead.

Tomasz Gawel

unread,
Jul 22, 2011, 12:28:30 PM7/22/11
to Google Web Toolkit
hi,

try something like this: (I'm not sure if there is any "built in"
solution but this "native patch" should work ;) )

public class ReadyStateWatch {
public static enum ReadyState {
COMPLETE {
@Override public String toString() { return "complete"; }
},
LOADING {
@Override public String toString() { return "loading"; }
},
INTERACTIVE {
@Override public String toString() { return "interactive"; }
},
UNINITIALIZED {
@Override public String toString() { return "uninitialized"; }
}
}
Frame iframe;

public ReadyStateWatch(Frame iframe){
this.iframe = iframe;
addNativeReadyStateWatch(this, iframe.getElement().<IFrameElement>
cast());
}

public HandlerRegistration addReadyStateChangeHandler(
ValueChangeHandler<ReadyState> handler) {
return iframe.addHandler(handler, ValueChangeEvent.getType());
}

public ReadyState getReadyState(){
try {
return
ReadyState.valueOf(iframe.getElement().getPropertyString("readyState"));
}
catch (Exception e) {
return null;
}
}

@SuppressWarnings("unused") //used in native function
private void fireReadyStateChange(){
iframe.fireEvent(new ValueChangeEvent<ReadyState>(getReadyState())
{});
}

private static native void addNativeReadyStateWatch(ReadyStateWatch
self, IFrameElement e) /*-{
var handleStateChange = function(){
self.@com.your.package.ReadyStateWatch::fireReadyStateChange()();
};
if (e.addEventListener) {
e.addEventListener("onreadystatechange", handleStateChange, false);
}else if (e.attachEvent) {
e.attachEvent("onreadystatechange",handleStateChange);
}else{
e.onreadystatechange=handleStateChange;
}
}-*/;
}

Tomasz Gawel

unread,
Jul 22, 2011, 12:29:11 PM7/22/11
to Google Web Toolkit
....and sample usage:

class UsageExample {
public static void useItLikeThat(){
Frame frame = new Frame("http://www.example.com/example.pdf");
new ReadyStateWatch(frame).addReadyStateChangeHandler(
new ValueChangeHandler<ReadyState>() {
@Override public void onValueChange(ValueChangeEvent<ReadyState>
event) {
switch(event.getValue()){
case COMPLETE:
Window.alert("I am loaded");
break;
case INTERACTIVE:
case LOADING:
case UNINITIALIZED:
}
}
});
}
}
Reply all
Reply to author
Forward
0 new messages