Take the 2-minute tour ×

So, if I have 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.

share|improve this question
Do you really want to do this? It'll cause a page refresh. –  seth Sep 9 '09 at 3:08
Is it possible to do without a page refresh? –  Horace Loeb Sep 9 '09 at 3:11
It is possible. AJAX history libraries deal with it. But it's not easy, and it has to be done differently for almost every browser. Not something you wanna get into. –  Gabriel Hurley Sep 9 '09 at 3:14
add comment

4 Answers

up vote 46 down vote accepted

Initial question:

window.location.href.substr(0, window.location.href.indexOf('#'))



both will return the URL without the hash or 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 for which is worse...

share|improve this answer
This is just plain wrong, you can change window.location.hash and it will not trigger a refresh. –  Evgeny Oct 31 '10 at 20:21
@Evgeny -- That's what my answer says. I explicitly say that changing window.location.hash won't trigger a 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)". –  Gabriel Hurley Oct 31 '10 at 22:29
No page reload - that is in the question. This is not the answer as it requires/forces a page reload! –  Ed_ May 28 '12 at 16:56
also think it should not be the ✓answer –  abernier Jun 19 '12 at 13:38
@abernier Agreed, doesn't say how do to remove the hash from the URL, just the URL string. –  Little Big Bot Apr 2 '13 at 17:01
show 5 more comments

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.

function removeHash () { 
    history.pushState("", document.title, window.location.pathname
                                                       + window.location.search);

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:

function removeHash () { 
    var scrollV, scrollH, loc = window.location;
    if ("pushState" in history)
        history.pushState("", document.title, loc.pathname + loc.search);
    else {
        // Prevent scrolling by storing the page's current scroll offset
        scrollV = document.body.scrollTop;
        scrollH = document.body.scrollLeft;

        loc.hash = "";

        // Restore the scroll offset, should be flicker free
        document.body.scrollTop = scrollV;
        document.body.scrollLeft = scrollH;

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 of pushState().

share|improve this answer
this should be the best answer –  Mauro Zadunaisky Mar 9 '12 at 21:38
Use this line to make sure you don't lose the query string: history.pushState("", document.title, window.location.pathname + window.location.search); –  Phil Kulak Mar 13 '12 at 15:56
@Phil: thanks for pointing that out, I've updated the answer accordingly. I'm too used to using pretty URLs. –  Andy E Mar 13 '12 at 17:32
For older versions of IE, it seems you need to use document.documentElement instead of document.body. See this answer stackoverflow.com/a/2717272/359048 –  jasonmcclurg Aug 14 '13 at 23:09
I suggest using replaceState instead of pushState, to not create an extra entry in the browser history. –  Nico Oct 6 '13 at 13:12
add comment

Have you tried the below?

share|improve this answer
yeah its working thanx @Vishal Sharma –  Jimmy Jul 27 '13 at 7:35
This doesn't answer the question if the user has come directly to the URL including the hash. –  nickb Jan 5 at 2:57
add comment

You can replace hash with null

var urlWithoutHash = document.location.href.replace(location.hash , "" );
share|improve this answer
add comment

Your Answer


