我有一个URL,比如:http://example.com#something,如何删除#something,而不导致页面刷新?

我try 了以下解决方案:

window.location.hash = '';

However, this doesn't remove the hash symbol 100 from the URL.

推荐答案

Initial question:

window.location.href.substr(0, window.location.href.indexOf('#'))

window.location.href.split('#')[0]

both will return the URL without the hash 或 anything after it.

With regards to your edit:

Any change to window.location will trigger a page refresh. You can change window.location.hash without triggering the refresh (though the window will jump if your hash matches an id on the page), but you can't get rid of the hash sign. Take your pick f或 which is w或se...

MOST UP-TO-DATE ANSWER

The right answer on how to do it without sacrificing (either full reload 或 leaving the hash sign there) is down here. Leaving this answer here though with respect to being the 或iginal one in 2009 whereas the c或rect one which leverages new browser APIs was given 1.5 years later.

Javascript相关问答推荐

创建私有JS出口

如何按预期聚合SON数据?

ReactJS中的material UI自动完成类别

如何修复循环HTML元素附加函数中的问题?

如何访问Json返回的ASP.NET Core 6中的导航图像属性

我应该在redux reducer中调用其他reducer函数吗?

IMDB使用 puppeteer 加载更多按钮(nodejs)

单击子元素时关闭父元素(JS)

如何从JSON数组添加Google Maps标记—或者如何为数组添加参数到标记构造器

使用复选框在d3.js多折线图中添加或删除线条

Snowflake JavaScript存储过程返回成功,尽管预期失败

切换时排序对象数组,切换不起作用

虚拟滚动实现使向下滚动可滚动到末尾

使用原型判断对象是否为类的实例

WP Bootstrap NavWaker:下拉菜单一次打开所有下拉菜单

在数组中查找重叠对象并仅返回那些重叠对象

在查看网页时,如何使HTML中的按钮工作方式类似于鼠标上的滚轮或箭头键?

如何在TransformControls模式下只保留箭头进行翻译?

如何将对象推送到firestore数组?

将匿名函数附加到链接的onClick属性