Remember scroll position RRS feed

  • Question

  • Hello,

    in our enterprise wiki I've added a scrollable div-container that contains lots of hyperlinks. When I click on one of these and push the back button of the browser firefox remember the scrollposition. Unfortunately IE and Google Chrome aren't. Any ideas how i can solve this - at least for IE?

    Thanks in advance.

    Wednesday, February 15, 2017 9:31 AM


All replies

  • e.g. here How can I retain the scroll position of a scrollable area when pressing back button there is good solution: save current scroll position on window unload and restore it on DOM ready. You will need to add here additional logic to distinguish scroll position for different pages. For doing that you may store scroll position e.g. in dictionary with page url as key.

    Blog - http://sadomovalex.blogspot.com
    Dynamic CAML queries via C# - http://camlex.codeplex.com

    Wednesday, February 15, 2017 2:04 PM
  • Hello,

    thanks for your answer. Unfortunately I'm not really a developer. Can you maybe help me out some more? I've inserted the div-container with the scroll-able content with the "Edit Source" Button.

    I've copied the code from the link you mentioned and inserted it with "embed code". No success...

    (Of course I've changed the div id before.)

    Thursday, February 16, 2017 7:56 AM
  • add output to browser console in order to check does it really saves scroll position:

    $(function() {
       $(window).unload(function() {
          var scrollPosition = $("div#element").scrollTop();
          localStorage.setItem("scrollPosition", scrollPosition);
          console.log("Current scroll position: " + scrollPosition);
       if(localStorage.scrollPosition) {
          console.log("Restored scroll position: " + localStorage.getItem("scrollPosition"));
    Browser console is available by F12 > Console.

    Blog - http://sadomovalex.blogspot.com
    Dynamic CAML queries via C# - http://camlex.codeplex.com

    Thursday, February 16, 2017 2:34 PM
  • It seems like the scroll position will be saved but when i press the back button it won't restore. But when i insert the code again in the console, then it reloads at the right positon

    Update: Finally it works. I just forgot to publish the js file :(

    Now I've a further question. What about the case, when a user simply leaves the page and comes back later. Right now the page even then remember the scroll position. Is there a way to fix this?

    • Edited by techNetAZV Friday, February 17, 2017 10:57 AM
    Thursday, February 16, 2017 3:04 PM
  • Monday, February 20, 2017 6:08 PM