javascript: completely remove top.location.hash?

为君一笑 提交于 2020-01-24 08:51:08

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!