如何清理window.location.hash?

时间:2014-05-14 18:14:19

标签: javascript jquery

我有一个有大量参数的方面,例如www.hello.com/kaleidoskop.html#look/2

如果你重新加载这一侧,你仍然会有参数。

我想删除它们,并在www.hello.com/kaleidoskop.html

刷新后开始

为此,我做了以下内容:

        function updateHash (t, clean) {
        if (clean) {
            var r = window.location.href.replace(/(javascript:|#).*$/, '');
            window.location.replace(r + '#' + t)
        } else window.location.hash = '#' + t
    }

但在这里我还有#。

如果我使用window.location.replace(r) or window.location.hash = r 它循环。

还有其他解决方案吗?

1 个答案:

答案 0 :(得分:2)

支持HTML5 History API的新浏览器:

history.pushState(null, null, window.location.href.split('#')[0]);

对于旧版浏览器(最好不用重新加载 - 仍保留#):

window.location.hash = '';