7

Hi I am using GWT and its standart way to support history via "History" class. It is very convenient but how can I remove anchor part from an url? For example:

My base url:

http://www.mysuperwebsite.com/myapp

While using application I move to a place that adds new history item.

In code:

History.newItem("funnygame");

As result:

http://www.mysuperwebsite.com/myapp#funnygame

I change place one more time:

In code:

History.newItem("notsofunnygames");

As result:

http://www.mysuperwebsite.com/myapp#notsofunnygames

Then I want to go back to my home page (http://www.mysuperwebsite.com/myapp).

What should be placed in code?:

????

to get back to:

http://www.mysuperwebsite.com/myapp

Is there any standart way I can achieve my goal?


If I add something like this:

History.newItem(""); 

or

History.newItem(null); 

the url will become

http://www.mysuperwebsite.com/myapp#

And this is not what I am lloking for, I need it without sharp character.

kapandron
  • 3,546
  • 2
  • 25
  • 38
Zalivaka
  • 763
  • 2
  • 13
  • 20

4 Answers4

5

If you use History.newItem(null); a new event will be fired. As a result, you will toggle your home page : http://www.mysuperwebsite.com/myapp#

Having or not a # at the end is the same thing, am I wrong ?

EDIT:

  ...
  // Get the last part of the url and remove #token
  String s = Location.getHref().substring(Location.getHref().lastIndexOf("/"));
  s = s.substring(0, s.indexOf("#")-1);
  setToken(s);
  ...

  protected native void setToken(String token) /*-{
    $wnd.history.pushState({},'', token);
}-*/;
Naaooj
  • 910
  • 8
  • 15
  • Thanks for your answer. This is the first solution one will think about. I need it without "#". I will update my question. – Zalivaka Jan 31 '11 at 14:59
  • The empty /myapp# fragment will have no effect, it's the same as /myapp in all ways. – Jason Hall Jan 31 '11 at 15:03
  • It is the same while we are trying to retrieve place names from it, but it looks different in the location bar. – Zalivaka Jan 31 '11 at 15:05
  • As far as I know, the only way to remove the # is to replace the url or loop back until you reach your home (without #). I guess you don't want the page to be reloaded, neither looping back (that would be stupid). I've no idea right now... – Naaooj Jan 31 '11 at 15:24
  • You could try to use history.pushState which is introduced in HTML5 ? See my answer edited... – Naaooj Jan 31 '11 at 16:05
  • Although it looks like a workaround a bit, it is certainly the thing I was looking for! Thanks a lot for your answer! – Zalivaka Feb 01 '11 at 15:49
2

The anchor is identified by a #, so you can use the following to remove it:

int index = link.indexOf('#');
if (index != -1) {
    link = link.substring(0, index);
}
dogbane
  • 266,786
  • 75
  • 396
  • 414
  • Thanks for your answer. But you haven't understood me. I have updated my question to be more clear. – Zalivaka Jan 31 '11 at 12:56
  • It is not suitable, cos I provided a very simple example, while it is far more complex in reality. A lot of places can be viewed before I need to go back to home view. – Zalivaka Jan 31 '11 at 13:27
0

You can create a tool class, and you call it when History changes.

public class UrlUpdater {

    public static void removeHashIfEmpty() {
        if(isHashEmpty())
            removeHash();
    }

    private static boolean isHashEmpty() {
        return "#".equals(Window.Location.getHash());
    }

    private static void removeHash() {
        updateURLWithoutReloading(Window.Location.createUrlBuilder().setHash(null).buildString());
    }

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

}
Daniel Hári
  • 7,254
  • 5
  • 39
  • 54
0

If you dont want a historytoken you are probably using history wrong. If you're not intending to fiddle with navigation i would recommend using GwtEvent / EventHandler. Which is essentially what gwt's History class does, in addition to linking those events to the navigation history.

Simon Landeholm
  • 319
  • 2
  • 12