最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript: completely remove top.location.hash? - Stack Overflow

programmeradmin11浏览0评论

If I have already a hash in my addressbar like e.g. domain#whatever and I call:

top.location.hash = "";

the #wathever is transformed into domain# without anything.

Is it possible to pletely remove the hash? So there is no # left.

Because if I call top.location.hash = ""; the page jumps to it's top, because a # is passed to the url. I want to prevent that.

If I have already a hash in my addressbar like e.g. domain.#whatever and I call:

top.location.hash = "";

the #wathever is transformed into domain.# without anything.

Is it possible to pletely remove the hash? So there is no # left.

Because if I call top.location.hash = ""; the page jumps to it's top, because a # is passed to the url. I want to prevent that.

Share Improve this question edited Dec 14, 2020 at 0:49 peterh 1 asked Mar 21, 2011 at 20:54 mattmatt 44.5k107 gold badges268 silver badges402 bronze badges 1
  • without reloading the page? I'd say no – Guillaume86 Commented Mar 21, 2011 at 20:59
Add a ment  | 

4 Answers 4

Reset to default 3

it's possible with history.pushState, e.g.:

history.pushState({}, '', './');

Of course it's IE<10 inpatible, but works for me :-)

top.location = ''

should do that, but it will cause a page reload. I don't think there's any way to remove it programmatically.

window.location = window.location.href.replace( /#.*/, "");

Unfortunately there is no way to reliably do so without causing the page to refresh, in which case you could use the location.href property.

发布评论

评论列表(0)

  1. 暂无评论