如何在没有页面刷新的情况下使用JavaScript从window.location(URL)中删除哈希? [英] How to remove the hash from window.location (URL) with JavaScript without page refresh?

查看:170
本文介绍了如何在没有页面刷新的情况下使用JavaScript从window.location(URL)中删除哈希?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下网址: http://example.com#something ,如何删除 #something ,而不会导致页面刷新?

I have URL like: http://example.com#something, how do I remove #something, without causing the page to refresh?

我尝试了以下解决方案:

I attempted the following solution:

window.location.hash = '';

但是,这不会删除哈希符号来自网址。

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

推荐答案

初步问题:

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

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

两者都将返回没有的URL哈希或其后的任何内容。

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

关于您的修改:

window.location 的任何更改都将触发页面刷新。您可以在不触发刷新的情况下更改 window.location.hash (尽管如果您的哈希与页面上的ID匹配,窗口将跳转),但您无法摆脱哈希标志。选择更糟糕的选择...

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 for which is worse...

最新的答案

关于如何在不牺牲的情况下做出正确答案(完全重新加载或在那里留下哈希标志)是在这里。将这个答案放在这里虽然是2009年的原始答案,而利用新浏览器API的正确答案是在1。5年后给出的。

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

这篇关于如何在没有页面刷新的情况下使用JavaScript从window.location(URL)中删除哈希?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆