Remove hash from url

I am ajax-ifying pagination in one of my projects, and since I want users to be able to add bookmarks to the current page, I add the page number via a hash, say:

onclick="callPage(2); window.location.hash='p=2'; return false;" 

and thats on hyperlink it works fine and that’s all, except when the page number is 1, I don’t want the URL be /products#p=1 , I just want it to be /products

I tried these options:

  • window.location.hash='' works, but the url now looks like /products# , and I'm not really a hash there.
  • do not use window.location.hash at all, but when the user returns to page 1, for example, on page 3, he is on the first page, but the url is still /products#p=3 , since I did not bother with the hash.
  • A Google search on this led me to several minutes (about 15) of stupid forums where the question was asked correctly, but the answers suggested that the page skipped because the creator of the stream had a hash in href like <a href="#"> , and it should use javascript:void(0) instead. (have they never heard of Ajax?)

So, finally, I decided to make this thread, here I found several similar threads, but all the answers are very similar to my second point.

so my big question still remains the question: how to remove the hash from the url and possibly from the universe? (only for the first page!)

+48
javascript ajax fragment-identifier
Dec 22 '10 at 11:13
source share
5 answers

Updated answer :

The best way to achieve this is to follow the Homero Barbosa answer below :

 history.pushState("", document.title, window.location.pathname); 

... or, if you want to save the search parameters:

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

Original answer, do not use this, badwrongfun :

 var loc = window.location.href, index = loc.indexOf('#'); if (index > 0) { window.location = loc.substring(0, index); } 

... but this refreshes the page for you, which seems a little rude after you just arrived there. Grin and the bear seem to be the best option.

+50
Dec 22 '10 at 11:35
source share
 history.pushState("", document.title, window.location.pathname); 
+87
Mar 10 '12 at 2:10
source share
 var urlWithoutHash = document.location.href.replace(location.hash , "" ); 
+5
Sep 27 '13 at 6:00
source share
 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 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; } } 
+3
Oct 28 '15 at 5:59
source share

Worked great for me

 $(window).on('hashchange', function(e){ window.history.pushState("", document.title, window.location.pathname); // do something... }); 
+3
Jun 16 '16 at 8:05
source share



All Articles