6

Can i change url(set parameter) without submit? I found this method http://google-web-toolkit.googlecode.com/svn/javadoc/2.1/com/google/gwt/user/client/Window.Location.html#replace%28java.lang.String%29 but it submit page. All GWT state will be lost.

user672649
  • 63
  • 1
  • 4

3 Answers3

10

If you want to change something that is not in the hash, for example you want to change a parameter in the URL, you can do it like this!

private void someMethod() {
    String newURL = Window.Location.createUrlBuilder().setParameter("someParam", "someValue").buildString();
    updateURLWithoutReloading(newURL);
}

private static native void updateURLWithoutReloading(String newUrl) /*-{
    $wnd.history.pushState(newUrl, "", newUrl);
}-*/;

Then you could register a function that handles the user using the back and forward browser buttons as demonstrated here.

Community
  • 1
  • 1
11101101b
  • 7,679
  • 2
  • 42
  • 52
7

Why are you trying to do this? Generally speaking, GWT apps don't change pages - thus they are normally SPAs (single page applications)

When you load a new page from a server, you will lose the state on that page. You can change the hash part of the URL as that won't return to the server, like this:

String newURL = Window.Location.createUrlBuilder().setHash("newhash").buildString();
Window.Location.replace(newURL);

However, if you're going to do this, I would recommend taking a look at GWT's MVP framework, which has built in support for managing locations using hash tokens.

http://code.google.com/webtoolkit/doc/latest/DevGuideMvpActivitiesAndPlaces.html

Andrew Newdigate
  • 6,005
  • 3
  • 37
  • 31
  • Thank you for useful answer. I need to change url without submit, so user can copy this and give to friend. For example wikimapia.org set in url current coordinates http://wikimapia.org/#lat=54.3885546&lon=25.9799194&z=11&l=0&m=b – user672649 Mar 23 '11 at 10:09
  • This helped my because I have a link that calls another GWT app. I'm using Window.Location.assign() Thanks guys! – agentcurry Mar 12 '13 at 18:28
2
 $wnd.history.pushState(newUrl, "", newUrl);

Works nicely in HTML5-browsers. Not in IE8 or IE9!