0

I'm currently trying to pass a page anchor from one page, to another, to have the second page scroll smoothly to the correct div.

Currently the second part of the desired feature works, but I'm looking for a way to pass the anchor to the second page without it appearing in the URL.

So for example, instead of having

www.example.com/blog#features

The anchor tag #features, could be passed somehow to the blog page and scroll correctly without it appearing in the URL.

Is this possible with jQuery or some form of Ajax style request?

AlbrightK
  • 73
  • 8
  • take a look at this answer: https://stackoverflow.com/questions/20215248/prevent-href-link-from-changing-the-url-hash – Robert Jul 17 '18 at 20:54

1 Answers1

1

try to use localStorage for example http://devdocs.io/dom/window/localstorage

window.localStorage.setItem('id_of_element_to_scroll', '#example-div');

Then you could read from it and scroll into view.

$(document).ready(function () {
  var idToScroll = localStorage.getItem('id_of_element_to_scroll');
  $('html, body').animate({
    scrollTop: $(idToScroll).offset().top
  }, 200); // with animation
})
  • Thank you, this works almost perfectly out of the box. I just needed to add `localStorage.clear();` from the devdocs page to ensure the item was removed, so page refreshes didn't keep returning it to the same spot of the page. Now it works perfectly for me. – AlbrightK Jul 18 '18 at 12:52