如何在没有页面刷新的情况下使用JavaScript从window.location(URL)中删除哈希?

Tom*_*man 306 javascript window.location fragment-identifier

我有这样的URL:http://example.com#something如何删除#something,而不会导致页面刷新?

我尝试了以下解决方案:

window.location.hash = '';
Run Code Online (Sandbox Code Playgroud)

但是,这不会#从URL中删除哈希符号.

And*_*y E 557

现在解决这个问题的范围更大.在HTML5历史API可以让我们操纵地址栏当前域范围内显示任何URL.

function removeHash () { 
    history.pushState("", document.title, window.location.pathname
                                                       + window.location.search);
}
Run Code Online (Sandbox Code Playgroud)

工作演示:http://jsfiddle.net/AndyE/ycmPt/show/

这适用于Chrome 9,Firefox 4,Safari 5,Opera 11.50 IE 10.对于不受支持的浏览器,您总是可以编写一个优雅降级的脚本,在可用的情况下使用它:

function removeHash () { 
    var scrollV, scrollH, loc = window.location;
    if ("pushState" in history)
        history.pushState("", document.title, loc.pathname + loc.search);
    else {
        // Prevent scrolling by storing the page's current scroll offset
        scrollV = document.body.scrollTop;
        scrollH = document.body.scrollLeft;

        loc.hash = "";

        // Restore the scroll offset, should be flicker free
        document.body.scrollTop = scrollV;
        document.body.scrollLeft = scrollH;
    }
}
Run Code Online (Sandbox Code Playgroud)

所以你可以摆脱哈希符号,但不是在所有浏览器中.

注意:如果要替换浏览器历史记录中的当前页面,请使用replaceState()而不是pushState().

  • 这应该是最好的答案 (27认同)
  • 我建议使用replaceState而不是pushState,以便不在浏览器历史记录中创建额外的条目. (26认同)
  • 使用此行确保您不会丢失查询字符串:history.pushState("",document.title,window.location.pathname + window.location.search); (9认同)
  • @Phil:谢谢你指出这一点,我已经相应地更新了答案.我太习惯使用漂亮的网址了. (6认同)

Gab*_*ley 194

初步问题:

window.location.href.substr(0, window.location.href.indexOf('#'))
Run Code Online (Sandbox Code Playgroud)

要么

window.location.href.split('#')[0]
Run Code Online (Sandbox Code Playgroud)

两者都将返回没有哈希的URL或其后的任何内容.

关于你的编辑:

任何更改都window.location将触发页面刷新.您可以在window.location.hash不触发刷新的情况下进行更改(尽管如果您的哈希值与页面上的ID匹配,窗口将跳转),但您无法摆脱哈希符号.选择哪个更糟......

最新的答案

关于如何在不牺牲的情况下做出正确答案(完全重新加载或在那里留下哈希标志)就在这里.在这里留下这个答案,虽然是2009年的原始答案,而利用新浏览器API的正确答案是在1.5年后给出的.

  • @Evgeny - 这就是我的回答.我明确地说改变window.location.hash不会触发刷新."你可以在不触发刷新的情况下更改window.location.hash(如果你的哈希与页面上的id匹配,窗口会跳转)". (60认同)
  • 这是完全错误的,您可以更改window.location.hash并且它不会触发刷新. (20认同)
  • 也认为它不应该是✓answer (9认同)
  • 这不是OP问题的答案. (4认同)
  • 没有页面重新加载 - 这就是问题所在.这不是答案,因为它需要/强制页面重新加载! (3认同)
  • 如果href有#,则substr和split方法都可以.但是如果href没有#,则substr方法返回空字符串,而split返回href. (2认同)

Pac*_*ier 68

(太多的答案是多余的和过时的.)现在最好的解决方案是:

history.replaceState(null, null, ' ');
Run Code Online (Sandbox Code Playgroud)

  • 在 TypeScript 中,第二个参数不允许为 null,因为该命令采用字符串。Mozilla 建议使用空字符串。 (5认同)
  • 如果要保留历史记录,请改用`history.pushState(null,null,'')`。 (4认同)
  • 解释为什么`state`和`title`参数最终传递`null`可能是有用的. (4认同)
  • 答案.非常感谢最简单,最新的答案. (3认同)
  • 这个问题以及其他问题是它不会触发 onhashchange,即使哈希现在是空的,而以前不是空的。 (2认同)

cpr*_*ack 40

简洁优雅:

history.replaceState({}, document.title, ".");  // replace / with . to keep url
Run Code Online (Sandbox Code Playgroud)

  • 感谢您的回答,我修改为`history.replaceState({},document.title,location.href.substr(0,location.href.length-location.hash.length));`用于我的用例. (5认同)
  • 这不会保留url查询. (5认同)
  • 如果要保留在同一目录中,请使用点而不是斜杠 (3认同)
  • 怎么可能得到6个赞成票呢? (2认同)
  • 我的用例也是替换而不是push,但这对我来说更有意义:`history.replaceState(null,document.title,location.pathname + location.search);` (2认同)

Rah*_*pta 15

要删除哈希,您可以尝试使用此功能

function remove_hash_from_url()
{
    var uri = window.location.toString();
    if (uri.indexOf("#") > 0) {
        var clean_uri = uri.substring(0, uri.indexOf("#"));
        window.history.replaceState({}, document.title, clean_uri);
    }
}
Run Code Online (Sandbox Code Playgroud)


MET*_*EAD 13

你可以这样做:

history.replaceState({}, document.title, window.location.href.split('#')[0]);
Run Code Online (Sandbox Code Playgroud)

  • 在这两种情况下,加星标的答案对我都不起作用,但这个答案却对我有用。谢谢! (2认同)

Chr*_*ena 12

这也将删除尾随哈希.例如:http://test.com/123#abc - > http://test.com/123

if(window.history.pushState) {
    window.history.pushState('', '/', window.location.pathname)
} else {
    window.location.hash = '';
}
Run Code Online (Sandbox Code Playgroud)

  • @kevgathuku,您可以使用 location.search 将它们添加回来,例如: ` window.history.pushState('', '/', window.location.pathname + window.location.search)` (3认同)

Cas*_*sey 6

const url = new URL(window.location);
url.hash = '';
history.replaceState(null, document.title, url);
Run Code Online (Sandbox Code Playgroud)

  • 尽管此代码可以回答问题,但提供有关此代码为何和/或如何回答问题的其他上下文,可以提高其长期价值。 (2认同)

Nib*_*eza 5

接下来呢?

window.location.hash=' '

请注意,请将哈希设置为单个空格而不是一个空字符串。


将哈希设置为无效的锚也不会导致刷新。如,

window.location.hash='invalidtag'

但是,我发现上述解决方案具有误导性。这似乎表明在给定位置上具有给定名称的锚点,尽管没有。同时,使用空字符串会导致页面移到顶部,这有时是不可接受的。使用空格还可以确保每当复制URL并添加书签并再次访问该URL时,页面通常将位于顶部,并且空格将被忽略。

而且,嘿,这是我对StackOverflow的第一个答案。希望有人觉得它有用,并且符合社区标准。

  • 将哈希设置为空白仍将#保留在URL的末尾,我认为目标是将其完全删除。 (7认同)

小智 5

function removeLocationHash(){
    var noHashURL = window.location.href.replace(/#.*$/, '');
    window.history.replaceState('', document.title, noHashURL) 
}

window.addEventListener("load", function(){
    removeLocationHash();
});
Run Code Online (Sandbox Code Playgroud)