How can I retain the scroll position of a scrollable area when pressing back button?

46,596

Solution 1

During page unload, get the scroll position and store it in local storage. Then during page load, check local storage and set that scroll position. Assuming you have a div element with id element. In case it's for the page, please change the selector :)

$(function() {
   $(window).unload(function() {
      var scrollPosition = $("div#element").scrollTop();
      localStorage.setItem("scrollPosition", scrollPosition);
   });
   if(localStorage.scrollPosition) {
      $("div#element").scrollTop(localStorage.getItem("scrollPosition"));
   }
});

Solution 2

I think we should save scroll data per page, also we should use session storage instead of local storage since session storge effects only the current tab while local storage shared between all tabs and windows of the same origin

$(function () {
            var pathName = document.location.pathname;
            window.onbeforeunload = function () {
                var scrollPosition = $(document).scrollTop();
                sessionStorage.setItem("scrollPosition_" + pathName, scrollPosition.toString());
            }
            if (sessionStorage["scrollPosition_" + pathName]) {
                $(document).scrollTop(sessionStorage.getItem("scrollPosition_" + pathName));
            }
        });

Solution 3

I had the same problem with a simple user interface consisting of a fixed menu div and a scrolling document div ("pxeMainDiv" in the code example below). The following solution worked for me in Chrome 47.0.2526.106 m and in Firefox 43.0.3. (My application is for use in-house and I did not need to cater for old versions of IE).

$(document).ready(function(){
    if (history.state) {
       $("#pxeMainDiv").scrollTop(history.state.data);
    }
    $("#pxeMainDiv").scroll(function() {
       var scrollPos = $("#pxeMainDiv").scrollTop();
       var stateObj = { data: scrollPos };
       history.replaceState(stateObj, "");
    });
});

On the div scroll event, the scroll position of the div is stored in the state object inside the browser history object. Following a press of the Back button, on the document ready event, the scroll position of the div is restored to the value retrieved from the history.state object.

This solution should work for the reverse navigation of an arbitrarily long chain of links.

Documentation here: https://developer.mozilla.org/en-US/docs/Web/API/History_API

Solution 4

When using window.history.back(), this is actually default browser functionality as user SD. has pointed out.

On a site I am currently building, I wanted the logo of the company to backlink to the index page. Since jQuery 3, $(window).unload(function() should be rewritten to $(window).on('unload', function(). My code looks like this (using Kirby CMS' php syntax):

<?php if ($page->template() == 'home'): ?>

<script>

$(function() {
 $(window).on("unload", function() {
    var scrollPosition = $(window).scrollTop();
    localStorage.setItem("scrollPosition", scrollPosition);
 });
 if(localStorage.scrollPosition) {
    $(window).scrollTop(localStorage.getItem("scrollPosition"));
 }
});

</script>

Solution 5

For anyone coming from react or anything similar to react router, here are two simple functions:

export function saveScrollPosition(context: any) {
  let path = context.router.route.location.pathname;
  let y = window.scrollY;
  sessionStorage.setItem("scrollPosition_" + path, y.toString());
}

export function restoreScrollPosition(context: any) {
  let path = context.router.route.location.pathname;
  let y = Number(sessionStorage.getItem("scrollPosition_" + path));
  window.scrollTo(0, y);
}
Share:
46,596

Related videos on Youtube

user2335065
Author by

user2335065

Updated on April 21, 2022

Comments

  • user2335065
    user2335065 about 2 years

    I have a long list of links inside a big scrollable div. Each time when a user click on a link then click the back button, it starts at the very top of the div. It is not user friendly to our users. Any ways to let the browser scroll to the previous position when pressing the back button?

    Thank you very much!

    • Brijesh Bhatt
      Brijesh Bhatt about 9 years
    • Manoj Dhiman
      Manoj Dhiman about 9 years
      store the position in cookies and after that use the cookie to scroll the page to exact position .
  • user2335065
    user2335065 about 9 years
    Thanks! It's good, but still one little thing. When clicking back, the browser initial starts at the top, and jump to the position after 0.5 seconds. Any way to shorten the time lapse so that it looks smoother?
  • mohamedrias
    mohamedrias about 9 years
    It's because the code executes after the dom is loaded. That's why there is a delay. Instead of placing the code in domContent loaded. You can try $("div#element").load(function() {}) and place the element outside the document ready handler.
  • user2335065
    user2335065 about 9 years
    Also, if a user scroll down the page, then leave the page, and go back to the page again, it should be a new start for him so it should start at the top. But now it also remembers the previous position. Any way to prevent this? :)
  • mohamedrias
    mohamedrias about 9 years
    Add one more condition, Check $(window).scrollTop(); and see if it's above 650. In that case don't store the scrollPosition in the local storage :)
  • mohamedrias
    mohamedrias about 9 years
    Also if the $(window).scrollTop(); is above 650, remove the scrollPosition from local storage as well.
  • Timo Ernst
    Timo Ernst over 7 years
    @mohamedrias Why 650?
  • mohamedrias
    mohamedrias over 7 years
    @Timo OP wanted to clear the scroll position when the user scrolls below a certain point so that next time again the page will start from the beginning.. That's what I remember from the last messages ;)
  • Jerry
    Jerry over 5 years
    @mohamedrias, very useful
  • Sascha Grindau
    Sascha Grindau almost 5 years
    can someone provide any vanilla versions that match 2019s compatibility? :D
  • Frits
    Frits over 4 years
    Yes - this is super important. Saving the pathname in sessionStorage is exactly what is needed here.
  • Hadi Pawar
    Hadi Pawar almost 3 years
    it would've been great to know how to use these.
  • Pedro
    Pedro almost 3 years
    Thanks @mohamedrias you save my day!