Class History

java.lang.Object
com.google.gwt.user.client.History

public class History extends Object
This class allows you to interact with the browser's history stack. Each "item" on the stack is represented by a single string, referred to as a "token". You can create new history items (which have a token associated with them when they are created), and you can programmatically force the current history to move back or forward.

In order to receive notification of user-directed changes to the current history item, implement the ValueChangeHandler interface and attach it via addValueChangeHandler(ValueChangeHandler).

Example

public class HistoryExample implements EntryPoint, ValueChangeHandler<String> {

  private Label lbl = new Label();

  public void onModuleLoad() {
    // Create three hyperlinks that change the application's history.
    Hyperlink link0 = new Hyperlink("link to foo", "foo");
    Hyperlink link1 = new Hyperlink("link to bar", "bar");
    Hyperlink link2 = new Hyperlink("link to baz", "baz");

    // If the application starts with no history token, redirect to a new
    // 'baz' state.
    String initToken = History.getToken();
    if (initToken.length() == 0) {
      History.newItem("baz");
    }

    // Add widgets to the root panel.
    VerticalPanel panel = new VerticalPanel();
    panel.add(lbl);
    panel.add(link0);
    panel.add(link1);
    panel.add(link2);
    RootPanel.get().add(panel);

    // Add history listener
    History.addValueChangeHandler(this);

    // Now that we've setup our listener, fire the initial history state.
    History.fireCurrentHistoryState();
  }

  public void onValueChange(ValueChangeEvent<String> event) {
    // This method is called whenever the application's history changes. Set
    // the label to reflect the current history token.
    lbl.setText("The current history token is: " + event.getValue());
  }
}

URL Encoding

Any valid characters may be used in the history token and will survive round-trips through newItem(String) to getToken()/ ValueChangeHandler.onValueChange(com.google.gwt.event.logical.shared.ValueChangeEvent) , but most will be encoded in the user-visible URL. The following US-ASCII characters are not encoded on any currently supported browser (but may be in the future due to future browser changes):
  • a-z
  • A-Z
  • 0-9
  • ;,/?:@&=+$-_.!~*()