tags:

views:

367

answers:

3

How do I make a Hyperlink for the current page fire the history change event?

For example, on http://localhost:8080/index.html#foo there is a Hyperlink with a historyToken of foo. How do I get the app to process/fire that click/change event?

Am I at the mercy of the nature of browser behavior?

A: 

Something like this fires this URL and adds it to the History

History.newItem("foo", true);

this just adds it to the History as a response to user actions:

History.newItem("foo", false);

That is install HistoryListener (onHistoryChanged), and react on that one only.

If user clicks a link/goes to favourites directly, respond from within onHistoryChanged.

When user does something and you need to change the URL, fire History.newItem(...,true), and again respond to it within onHistoryChanged.

Vladimir Dyuzhev
+1  A: 

To get your app to respond to the click of the Hyperlink you can add a ClickHandler to the Hyperlink. The ClickHandler should do the work of updating your app interface, etc. The history token will get updated automatically based on the history token set in the constructor of the Hyperlink and the History.newItem() method does not need to be called.

To handle arbitrary URL's with your possible tokens being pasted into url field of the browser or bookmarked by your users you need to implement a ValueChangeHandler. Check the documentation of HyperLink for an example.

Carnell
This seems like the right answer.
antony.trupe
Can you mark the answer as correct if you believe it to be the correct one. I would appreciate it. Also, good luck with your app.
Carnell
Once I get a chance to prove it works, I will most definitely accept the answer.
antony.trupe
it doesn't work. see bug http://code.google.com/p/google-web-toolkit/issues/detail?id=4186
antony.trupe
A: 
Hyperlink myLink = new Hyperlink("foo");
myLink.addClickHandler(new ClickHandler() {
  @Override
  public void onClick(ClickEvent event) {
    String currentToken = History.getToken();
    String newToken = ((Hyperlink) event.getSource()).getTargetHistoryToken();
    if (currentToken == newToken) {
      History.fireCurrentHistoryState();
    }
  }
});
antony.trupe