tags:

views:

178

answers:

2

In my GWT application, I want to ask a user confirmation when he navigates out of the current application, i.e. by entering a URL or closing the browser. This is typically done by registering a ClosingHandler and setting the desired dialog message in the onWindowClosing method. This seems to work well.

However, if the user tries to navigate say to http://www.gmail.com (by typing it in the URL bar) and hits Cancel to indicate he doesn't want to navigate, then my app keeps running but the browser's URL bar keeps indicating http://www.gmail.com. This causes a number of problems later in my application and will give the wrong result if the user bookmarks the page.

Is there a way to automatically reset the URL when the user presses Cancel?

Or, alternatively, is there a way to detect the user pressed the Cancel button? If so, is there a way to set the URL without triggering a ValueChangeEvent? (I could add some logic to prevent this, but I'd rather use a built-in mechanism if it exists.)

+3  A: 

Not sure if this works but did you try: History.newItem(History.getToken(), false); to reset the URL? It does set the history token without triggering a new history item.

Hilbrand
Thanks! This doesn't work exactly, but by storing the previous token I'm able to solve the problem when the user navigates within the application (because in this case, I know when the user presses Cancel). Now if only I had a way of knowing after the onWindowClosing dialog. Maybe with a timer? (yuck!)
Philippe Beaudoin
+1  A: 

I managed to do this. It looks like GWT DeferredCommand are executed after the confirmation window has been closed. This, combined with Hilbrand's answer above, give me exactly what I want. Here is exactly what I do:

  public final void onWindowClosing(Window.ClosingEvent event) {
    event.setMessage(onLeaveQuestion);
    DeferredCommand.addCommand( new Command() {
      public void execute() {
        Window.Location.replace(currentLocation);
      }
    });
  }

Where currentLocation is obtained by calling Window.Location.getHref() every time the history token changes.

Philippe Beaudoin