I have URL like: http://example.com#something
, how do I remove #something
, without causing the page to refresh?
I attempted the following solution:
window.location.hash = '';
However, this doesn't remove the hash symbol #
from the URL.
I have URL like: http://example.com#something
, how do I remove #something
, without causing the page to refresh?
I attempted the following solution:
window.location.hash = '';
However, this doesn't remove the hash symbol #
from the URL.
Solving this problem is much more within reach nowadays. The HTML5 History API allows us to manipulate the location bar to display any URL within the current domain.
Working demo: http://jsfiddle.net/AndyE/ycmPt/show/
This works in Chrome 9, Firefox 4, Safari 5, Opera 11.50 and in IE 10. For unsupported browsers, you could always write a gracefully degrading script that makes use of it where available:
So you can get rid of the hash symbol, just not in all browsers — yet.
Note: if you want to replace the current page in the browser history, use
replaceState()
instead ofpushState()
.This will remove the trailing hash as well. eg: http://test.com/123#abc -> http://test.com/123
You can replace hash with null
You can do it as below:
history.replaceState({}, document.title, window.location.href.split('#')[0]);
Try the following:
To remove the hash, you may try using this function