Window onfocus/onblur events.

1,894 views
Skip to first unread message

Ayzen

unread,
Sep 6, 2006, 4:44:48 PM9/6/06
to Google Web Toolkit
Hi.

Does anybody know how to handle window onfocus/onblur events? I tried:
$wnd.onfocus = function() {
alert("Test");
};
in native method, but it doesn't work in IE. In Opera and FF it works
fine.

Thanks.

Eric B

unread,
Sep 6, 2006, 11:43:03 PM9/6/06
to Google Web Toolkit
Check out
http://code.google.com/webtoolkit/documentation/com.google.gwt.user.client.EventListener.html#onBrowserEvent(com.google.gwt.user.client.Event).
This would allow you to capture all the events and filter out the
onFocus and onBlur events specifically. Haven't tested this, so just
pure conjecture. :)

Thanks,
Eric

Ayzen

unread,
Sep 7, 2006, 4:26:04 AM9/7/06
to Google Web Toolkit
Hi, Eric.

I tried DOM.addEventPreview to catch these events, but onblur event
doesn't fire correctly when window lost focus. :(

Ayzen

unread,
Sep 8, 2006, 7:52:35 AM9/8/06
to Google Web Toolkit
It will be great if GWT developers will add addWindowBlurListener and
addWindowFocusListener to Window class in next release.

Eric B

unread,
Sep 8, 2006, 10:36:59 AM9/8/06
to Google Web Toolkit
You can probably add it yourself in the interum. I think it would be
something like this (note, did not test):

public class WindowEventManager {

private static final WindowEventManager instance = new
WindowEventManager();

protected List listeners;

private WindowEventManager() {
WindowEventManager.initialize();
this.listeners = new ArrayList();
}

private native static void initialize() /*-{
$wnd.onblur = new function( e ) {
@packagepath.WindowEventManager::getInstance()().notifyOnBlur( e );
}

$wnd.onfocus = new function( e ) {
@packagepath.WindowEventManager::getInstance()().notifyOnFocus( e );
}
}-*/;

public static void getInstance() {
return WindowEventManager.instance;
}

private void notifyOnFocus( Event e ) {
for ( Iterator i = this.listeners.iterator(); i.hasNext(); ) {
((WindowEventListener) i.next()).onFocus( e );
}
}

private void notifyOnBlur( Event e ) {
// duplicate notifyOnFocus, but call the onBlur method
}

public void addListener( WindowEventListener wel ) {
this.listeners.add( wel );
}

public void removeListener( WindowEventListener wel ) {
this.listeners.remove( wel );
}

}

public interface WindowFocusListener() {

public void onFocus( Event e );
public void onBlur( Event e );
}

Thanks,
Eric

Ayzen

unread,
Sep 8, 2006, 11:29:35 AM9/8/06
to Google Web Toolkit
Eric, the main problem is that $wnd.onblur = new function( e ) does not
work in IE. :(

Eric B

unread,
Sep 8, 2006, 12:21:33 PM9/8/06
to Google Web Toolkit
Do you have a doctype in your html file? Try removing it if you do and
see if it fires then. If you don't, try adding one.

The problem comes when you need a doctype for one thing to work, but
need to remove it for another to work. :) I haven't figured out how
to manage that.

Thanks,
Eric

Ayzen

unread,
Sep 8, 2006, 5:02:15 PM9/8/06
to Google Web Toolkit
Eric, thanks for your replies.

I don't have doctype. But I noticed if Window.onblur = ... located in
gwt.js events fire in IE also.

Eric B

unread,
Sep 8, 2006, 5:26:14 PM9/8/06
to Google Web Toolkit
Sorry I don't understand what you're saying. Can you elaborate what
you mean by "But I noticed if Window.onblur = ... located in gwt.js
events fire in IE also."?

Thanks,
Eric

Ayzen

unread,
Sep 9, 2006, 4:27:01 AM9/9/06
to Google Web Toolkit
If I put this code:
"window.onblur = new function() {
someHandler();
}"
in gwt.js file - blur event works in IE.
If I put this code(window replaced with $wnd) in ***.cache.html files -
blur event doesn't work in IE(but works in FF and Opera).

Sorry for my poor English.

Nano Elefant

unread,
Apr 4, 2013, 8:20:45 AM4/4/13
to google-we...@googlegroups.com, Google Web Toolkit, ay...@p5com.com
I'm struggling now since days trying to determine if the browser window is inactive. There's probably a reason why GWT does not support this, but I must implement it non the less. Can you please tell me how it can be done?
Eric's "code" above, does not even call the notifyOnFocus/notifyOnBlur methods (OSX, neither Chrome nor FF), so I need an other (i.e. working) solution.
Any advice is welcome.
Thank you.

Jens

unread,
Apr 4, 2013, 8:52:01 AM4/4/13
to google-we...@googlegroups.com, Google Web Toolkit, ay...@p5com.com
Check out http://stackoverflow.com/a/1060034

Its pure JavaScript but it shouldn't be hard to adapt it to GWT.

-- J.
Reply all
Reply to author
Forward
0 new messages