Good evening. Such a question is how to remove # (hash / grid) in the URL when scrolling to the anchor for this example:

$(document).ready(function () { $(document).on("scroll", onScroll); $("a[href^=#]").click(function(e){ e.preventDefault(); $(document).off("scroll"); $(menu_selector + " a.active").removeClass("active"); $(this).addClass("active"); var hash = $(this).attr("href"); var target = $(hash); $("html, body").animate({ scrollTop: target.offset().top }, 900, function(){ window.location.hash = hash; $(document).on("scroll", onScroll); }); }); }); 
  • one
    No need to clean up. - Qwertiy
  • why not? - vladislav_zp
  • 3
    Because the url with a grid can be saved to bookmarks, or forwarded to another person - in this case, the necessary section will be opened immediately. - Qwertiy
  • What is stopping you lattice? - t1nk
  • This is a landing page, then if you scroll a little, it's okay, and when there is a hash, it looks like the "past century" - vladislav_zp

1 answer 1

I want to join the commentators and support the view that there is no need to remove the "anchor". It allows you to get a link to a specific part of the page and does not look like the "past century" at all, what a strange prejudice.

In addition, it is strange and illogical to use technology (right for its intended purpose) and get rid of half of its “purpose”. Why not just scroll the page using javascript when clicking on an item? And there will be no hash.

One way or another, the question requires an answer. It’s up to you how to use the available tools.

You can remove an anchor without reloading the page, as far as I know, only with the help of the History API, for example, with such code:

 if(window.history && history.pushState) history.pushState({foo: 'bar'}, 'Title', location.href.replace(location.hash,'')); 

Verification is required, since older browsers do not support this functionality. Although, fortunately, at the moment the absolute majority of users use compatible browsers.

  • Checking window.history superfluous, because history.back() is a very old thing, so the history object will definitely be. Enough to check history.pushState . - Qwertiy