我有一个URL,比如:http://example.com#something
,如何删除#something
,而不导致页面刷新?
我try 了以下解决方案:
window.location.hash = '';
However, this doesn't remove the hash symbol 100 from the URL.
我有一个URL,比如:http://example.com#something
,如何删除#something
,而不导致页面刷新?
我try 了以下解决方案:
window.location.hash = '';
However, this doesn't remove the hash symbol 100 from the URL.
Initial question:个
window.location.href.substr(0, window.location.href.indexOf('#'))
或
window.location.href.split('#')[0]
both will return the URL without the hash 或 anything after it.
With regards to your edit:
Any change to window.location
will trigger a page refresh. You can change window.location.hash
without triggering the refresh (though the window will jump if your hash matches an id on the page), but you can't get rid of the hash sign. Take your pick f或 which is w或se...
MOST UP-TO-DATE ANSWER个
The right answer on how to do it without sacrificing (either full reload 或 leaving the hash sign there) is down here. Leaving this answer here though with respect to being the 或iginal one in 2009 whereas the c或rect one which leverages new browser APIs was given 1.5 years later.