Adding a Close Event When the User Closes the Browser in GWT

Often times your GWT app will queue up client side RPC events. What if the user clicks close on the browser window or tries to navigate to a new page? The solution is very simple.

Window.addWindowClosingHandler(new ClosingHandler(){
    @Override public void onWindowClosing(ClosingEvent event) {
        event.setMessage("If you leave, you may lose data. Continue?");
    }
});

Simple as pie. If event.setMessage is called, a prompt will be displayed for you.

If you made it this far, you should follow me on Twitter.

-JP

comments powered by Disqus