Is it possible to automatically change the url example.com/4000/title-2/#!4000 to example.com/4000/title-2 without to refresh the page ? Basically to remove "/#!4000" from the URL. Note that is important to remove the "/" before the hashbang not just the hashbang .
1 Answer
dont know if it is enough for you and whether it works completely cross-browser... chrome accepts:
location.hash = "";
but this keeps the "#" in the address bar
in modern browsers that completely support the html5 history api you do:
window.history.replaceState('Object', 'Title', '/4000/title-2');
EDIT: this dies not change the history of the browser
EDIT 2: just found this stackoverflow resource
8 Comments
themihai
we need to remove # and also the "/" before it .
sg3s
@mihai it is impossible without a page refresh.
David Thomas
MDN entry for browser history, and W3 entry for
replaceState(). And +1 for awesome, I hadn't come across that before =)Tobias Krogh
I agree, HTML5 is offering awesome APIs :)
Tobias Krogh
@WaleedAhmad
router.navigate sounds like you are in an Angular context? If so you might be able to fetch the current state using getState() via Location and pass that to the new entry using replaceState()... generally speaking it might make sense to use the Angular APIs rather than native ones here but that also depends on your router settings... does this help? |
hrefof a link element, orwindow.location? I'd suggest, if you want to change the URL in the browser's address-bar, that you look at url-rewriting, with whatever you've got running on your server (Apache?).