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.
Here is another solution to change the location using href and clear the hash without scrolling.
The magic solution is explained here. Specs here.
NOTE: The proposed API is now part of WhatWG HTML Living Standard
Simple and elegant:
Initial question:
or
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 changewindow.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...MOST UP-TO-DATE ANSWER
The right answer on how to do it without sacrificing (either full reload or leaving the hash sign there) is down here. Leaving this answer here though with respect to being the original one in 2009 whereas the correct one which leverages new browser APIs was given 1.5 years later.
put this code on head section
(Too many answers are redundant and outdated.) The best solution now is this: