最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin5PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far