从URL中完全删除哈希

Completely remove hash from URL

本文关键字:删除 哈希 URL      更新时间:2023-09-26

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

页面链接:

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

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

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

根据您的具体上下文,有几种方法可以实现这一点。

如果你只想删除散列值:

    location.hash = '';

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

    location = location.pathname;

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

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

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

这在一些旧浏览器(包括IE 9)中不受支持,但这些浏览器正在迅速消失。