There are actually two methods you can use:
- Window.addWindowClosingHandler: Registers for
ClosingEvent
, which is fired by JavaScript's $wnd.onbeforeunload
.
- Window.addCloseHandler: Registers for
CloseEvent
, which is fired by JavaScript's $wnd.onunload
.
see WindowImpl.initWindowCloseHandler()
, and also WindowImplIE
The ClosingEvent is fired a little bit earlier than the CloseEvent.
(There's a third method, Window.addWindowCloseListener
, which effectively registers both for $wnd.onbeforeunload
and $wnd.onunload
, but that method is deprecated.)
The reason why you don't see the alert is probably explained with the javadoc of ClosingHandler.onWindowClosing()
:
* Fired just before the browser window closes or navigates to a different
* site. No user-interface may be displayed during shutdown.
And instead of Window.location.replace()
you should rather make an ajax call for the logout (e.g. using GWT-RPC, or RequestBuilder). The user closes the window, so it can't replace its location anymore.