3

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

top.location.hash = "";

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

Is it possible to completely 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.

peterh
  • 1
  • 15
  • 76
  • 99
matt
  • 40,185
  • 99
  • 257
  • 397

4 Answers4

3

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

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

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

Dziad Borowy
  • 12,045
  • 4
  • 38
  • 51
2
top.location = ''

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

None
  • 1
  • 30
  • 155
  • 213
  • ok, is it possible then to just set it "#" but don't make the page jump to it's top? I wouldn't mind if there is a # left in the url. I just don't want my site to jumpt to the top. – matt Mar 21 '11 at 21:04
  • http://stackoverflow.com/questions/2295845/remove-hash-without-page-jump should answer your question. – Johan Mar 21 '11 at 21:34
0
window.location = window.location.href.replace( /#.*/, "");
Peter Olson
  • 131,160
  • 48
  • 197
  • 239
0

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.

Zikes
  • 5,848
  • 1
  • 29
  • 44