繁体   English   中英

从网址完全删除哈希

[英]Completely remove hash from URL

我想知道如何从URL中删除ID。 像这样:

链接到页面:

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

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

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

有多种方法,具体取决于您的具体情况。

如果只想删除哈希值:

    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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM