问题
hey guys, quick question, I couldn't find anything useful on the web.
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.
回答1:
top.location = ''
should do that, but it will cause a page reload. I don't think there's any way to remove it programmatically.
回答2:
it's possible with history.pushState, e.g.:
history.pushState({}, '', './');
Of course it's IE<10 incompatible, but works for me :-)
回答3:
window.location = window.location.href.replace( /#.*/, "");
回答4:
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.
来源:https://stackoverflow.com/questions/5383591/javascript-completely-remove-top-location-hash