从URL中完全删除哈希

时间:2014-09-24 14:22:36

标签: javascript jquery

我想知道如何从网址中删除id。 像这样:

链接到页面:

http://www.exampledomain.com/index.html#example

当用户点击链接时,它变为:

http://www.exampledomain.com/index.html

1 个答案:

答案 0 :(得分:1)

根据您的具体情况,有几种方法。

如果您只想删除hash值:

    location.hash = '';

...但这会将#留在该位置。 (它还会将用户滚动到页面顶部。)要删除它:

    location = location.pathname;

...但这也会重新加载页面。解决所有这些问题:

    history.pushState({},'',location.pathname); 
    // back button returns to #example

    history.replaceState({},'',location.pathname); 
    // back button doesn't return to #example

... isn't supported in some old browsers(包括IE 9),但那些正在迅速消失。

相关问题