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
  • 11,875
  • 18
  • 85
  • 108
matt
  • 42,713
  • 103
  • 264
  • 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,368
  • 4
  • 41
  • 53
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.

  • 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
  • 139,199
  • 49
  • 202
  • 242
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,888
  • 1
  • 30
  • 44