Hello everybody! Please tell me if it is possible to do the following: there is a page http://name.ru/cat/1.html . This page has a link to another page http://name.ru/cat/2.html#hash . When clicking on a link, naturally, in the browser line, the address is displayed as http://name.ru/cat/2.html#hash . So is it possible that when clicking on a link in a browser line, the address is displayed without the #hash grid? Thank you for your comments!

  • what end url do you want to see? - Grundy
  • Ideally, of course, you need http: //name.ru/cat/ But if this is impossible in principle, then at least http: //name.ru/cat/2.html, i.e. without #hash. Unless of course this is possible - LADYX

2 answers 2

Look towards the HTML5 History API. Example of use for your case:

history.replaceState({}, '', location.href.replace(location.hash, '')) 

Here, the current state of the history is replaced with the same url, but with cut parameters. However, from the transition to the anchor it does not save. To do this, you will need to either initially open the page without an anchor, or reload it after replacing the state.

UPD : Intercept and replace url.

 $('.target-link').click(function(e) { var targetUrl = $(this).attr('href'), sharpIndex = targetUrl.indexOf('#'); if (sharpIndex >= 0) { e.preventDefault(); location.href = targetUrl.substr(0, sharpIndex); } }); 
  • In other words, it is simply impossible. I understand correctly? Initially the page will not open, because it opens at the address with the anchor. Instant reboot immediately after the initial boot - not even something that is not an option, but loses all meaning and defies any logic. Right? - LADYX
  • one
    Right. You can intercept click on the link, take its target address, remove the anchor and manually navigate to the page. - Weks
  • All is clear, thank you! Then another question arises, since it is impossible to do what is necessary in this way. I will be glad to comment. Stackoverflow.com/questions/502446/… - LADYX

You can use javascript to remove. Somewhere I got this jquery code:

 $('a[href*=#]').bind("click", function(e){ var anchor = $(this); $('html, body').stop().animate({ scrollTop: $(anchor.attr('href')).offset().top-100 }, 1000); e.preventDefault(); }); 

Or you can advise as here .

  • link to another page in question, not just to anchor - Grundy
  • The code that you gave me, it only works on one page, when you go to the anchor. And when you click on the link with the anchor and load this page, the anchor will still remain. - LADYX
  • According to the link expange.ru/e/How_tobate_yakor_(JavaScript) moved, looked. Yes, when adding this line there is no result, I have already tried this method. Here you can see how the page should already be loaded with the address without an anchor. - LADYX