Remove fragment in URL with JavaScript w/out causing page reload
Solution 1:
As others have mentioned, replaceState in HTML5 can be used to remove the URL fragment.
Here is an example:
// remove fragment as much as it can go without adding an entry in browser history:
window.location.replace("#");
// slice off the remaining '#' in HTML5:
if (typeof window.history.replaceState == 'function') {
history.replaceState({}, '', window.location.href.slice(0, -1));
}
Solution 2:
Since you are controlling the action on the hash value, why not just use a token that means "nothing", like "#_" or "#default".
Solution 3:
You could use the shiny new HTML5 window.history.pushState
and replaceState
methods, as described in ASCIIcasts 246: AJAX History State and on the GitHub blog. This lets you change the entire path (within the same origin host) not just the fragment. To try out this feature, browse around a GitHub repository with a recent browser.
Solution 4:
There is also another option instead of using hash,
you could use javascript: void(0);
Example: <a href="javascript:void(0);" class="open_div">Open Div</a>
I guess it also depends on when you need that kind of link, so you better check the following links:
How to use it: http://www.brightcherry.co.uk/scribbles/2010/04/25/javascript-how-to-remove-the-trailing-hash-in-a-url/ or check debate on what is better here: Which "href" value should I use for JavaScript links, "#" or "javascript:void(0)"?