0

Similar to my question here is there any way to prevent automatic scroll without a hash / anchor up top. The method described in that post revolves around checking if there is a hash.

Is there a way to know if the browser has a point to scroll to OR if the page is being refreshed or just visited.

Community
  • 1
  • 1
ThomasReggi
  • 55,053
  • 85
  • 237
  • 424

1 Answers1

2

If I understand you right, localStorage or sessionStorage could be helpful.

Just cache the current scroll-offset if the page is reloaded (listening to beforeunload or unload) and then restore it when the page loads:

var $window = $(window).on({
  beforeunload: function() {
    window.sessionStorage.scrollLeft = $window.scrollLeft()
    window.sessionStorage.scrollTop = $window.scrollTop()
  },
  load: function() {
    $window.scrollLeft(window.sessionStorage.scrollLeft)
    $window.scrollTop(window.sessionStorage.scrollTop)
  }
})
yckart
  • 32,460
  • 9
  • 122
  • 129