Search code examples
htmlback-buttonbrowser-history

Link within the same page, leaving history


I have a link <a href=...> that points to an anchor on the same page with an id, using #. After traversing with that link, when I go through browser history using backward/forward button on the browser, the browser does not scroll the page to the anchored position. Can I let it scroll?


Solution

  • Yes, you can, using JavaScript. In the script, write this:

    window.onhashchange = function(){
      location.href = location.hash;
    };