总重量。删除锚点部分 url

发布于 2024-10-15 08:31:59 字数 969 浏览 6 评论 0原文

您好,我正在使用 GWT 及其标准方式通过“History”类支持历史记录。这很方便,但是如何从网址中删除锚点部分?例如:

我的基本网址:

http://www.mysuperwebsite.com/myapp

在使用应用程序时,我移动到添加新历史记录项目的位置。

在代码中:

History.newItem("funnygame");

结果:

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

我再换一次地方:

在代码中:

History.newItem("notsofunnygames");

结果:

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

然后我想回到我的主页(http://www.mysuperwebsite.com/myapp)。

代码中应该放置什么?:

????

回到:

http://www.mysuperwebsite.com/myapp

有什么标准方法可以实现我的目标吗?


如果我添加这样的内容:

History.newItem(""); 

或者

History.newItem(null); 

网址将变成

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

这不是我想要的,我需要它而不需要尖锐的字符。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

最终幸福 2024-10-22 08:31:59

如果您使用 History.newItem(null); 将触发一个新事件。
结果,您将切换您的主页:
http://www.mysuperwebsite.com/myapp#

末尾有或没有 # 是一回事,我错了吗?

编辑:

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

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);
}-*/;
滴情不沾 2024-10-22 08:31:59

该锚点由 # 标识,因此您可以使用以下命令将其删除:

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

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);
}
寄人书 2024-10-22 08:31:59

如果您不需要历史令牌,您可能错误地使用了历史记录。如果您不打算摆弄导航,我建议使用 GwtEvent / EventHandler。除了将这些事件链接到导航历史记录之外,这本质上就是 gwt 的 History 类所做的事情。

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.

流心雨 2024-10-22 08:31:59

您可以创建一个工具类,并在 历史改变。

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

}

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文