Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change in URL to trigger Event in GWT

Tags:

gwt

I am iterating through some data and generating parameterized URL links based on some conditions like this:

finishedHTML.append("<a href=\"http://" + domain + "&service=" + allEvents[eventCounter].EventService +"&day="+ offSet+(-1 * i) +"\"><img src=\"/info.jpg\" alt=\"Info\"/>");

I had the beginning of my application checking for URL parameters when the page was loaded/refreshed, and take some action depending on if the parameters were there and what they were.

However, I added a # to the beginning of the paramters, so the page wouldn't need to be refreshed, but now it's not triggering the function that checks the URL.

My question is how can I trigger an event in GWT when a user clicks a link? Do I need to generate GWT controls, and link a click handler? Is it possible to setup an event that fires when the URL is changed???

UPDATE: Adam answered part of the initial question, but I can't get the querystring after "#". Is there a better way than the function below:

public static String getQueryString() 
 {
     return Window.Location.getQueryString();
 }

In other words, if I enter example.com?service=1 I get service=1 as my querystring. If I enter example.com#?service=1, I get a null value for the querystring..

like image 222
tpow Avatar asked Dec 12 '22 20:12

tpow


1 Answers

Use History.addValueChangeHandler( handler ), and create a handler to catch the URL changes.

There's no need for click handlers etc., any change of the "hash" part in the URL will be sufficient.

EDIT:

See this code example - it will parse URLs of the form http://mydomain/my/path#tok1&tok2&tok3

public void onValueChange(ValueChangeEvent<String> event) {
  String hash = event.getValue();
  if ( hash.length() == 0 ) {
    return;
  }
  String[] historyTokens = hash.split("&",0);
  // do stuff according to tokens
}
like image 160
adamk Avatar answered Dec 17 '22 10:12

adamk