GWT. Delete URL of anchor part

Hi, I am using GWT and its standard way of supporting history through the History class. This is very convenient, but how can I remove the anchor part from the URL? For example:

My base URL:

http://www.mysuperwebsite.com/myapp 

When using the application, I go to a place that adds a new story element.

In code:

 History.newItem("funnygame"); 

As a result:

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

I change the place again:

In code:

 History.newItem("notsofunnygames"); 

As a result:

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

Then I want to return to my homepage (http://www.mysuperwebsite.com/myapp).

What needs to be put in the code ?:

 ???? 

to return to:

 http://www.mysuperwebsite.com/myapp 

Is there a standard way that I can achieve my goal?


If I add something like this:

 History.newItem(""); 

or

 History.newItem(null); 

url will become

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

And this is not what I want, I need it without a harsh character.

+7
source share
4 answers

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

Having or not # at the end is the same, 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); }-*/; 
+5
source

An anchor is identified with # , so you can use the following to remove it:

 int index = link.indexOf('#'); if (index != -1) { link = link.substring(0, index); } 
+2
source

If you do not need a story, you are probably misusing the story. If you are not going to play with navigation, I would recommend using GwtEvent / EventHandler. In essence, this is the gwt History class, in addition to binding these events to navigation history.

0
source

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); }-*/; } 
0
source

All Articles