0

I have several anchor links on my webpage. I'd like to make it so that when the user refreshes/reloads the page, the page does not follow the anchor link in the URL, and goes to the top of the page. I have tried stripping everything after the # on the backend, but I can't imagine there isn't a more elegant solution. Is there a way to do this on the front-end, using HTML/JS?

Ivan Vegner
  • 1,707
  • 4
  • 14
  • 23
  • you want the browser to follow your anchors when someone reload or refreshes the page ? – M.Tanzil Jun 24 '16 at 23:08
  • If you want to do something when the page loads you could put something on the load event: https://developer.mozilla.org/en-US/docs/Web/Events/load – Ricky Nelson Jun 24 '16 at 23:12
  • Is this browser dependent? Neither Chrome nor Firefox seem to follow the anchors when I refresh, they stay in the same location you were scrolled to previously. – ryan Jun 24 '16 at 23:15
  • 2
    `$(window).on('beforeunload', function(){$(window).scrollTop(0);});` – Adam Buchanan Smith Jun 24 '16 at 23:17
  • @M.Tanzil I **do not** want to follow the anchor links. I want it to behave as if the anchor links weren't in the URL at all. – Ivan Vegner Jun 24 '16 at 23:39
  • So you want when there is click on the links there is # added to the URL and the page goes to the top, and you want the page don't need to go to the top of page but remain as it is ? – M.Tanzil Jun 24 '16 at 23:53
  • are you looking for [this](http://stackoverflow.com/questions/3659072/how-to-disable-anchor-jump-when-loading-a-page)? – tmg Jun 24 '16 at 23:54

0 Answers0