I can not understand why the scroll does not remember its position.

Memorization works on the page /ajax/select/dialog.php

<script> window.addEventListener('DOMContentLoaded', function() { var div = document.querySelector('#slider'); var scroll = localStorage.getItem('scroll')||0; div.scrollTop = scroll; div.addEventListener('scroll', function() { localStorage.setItem('scroll', div.scrollTop); }) }); </script> 

Next, on another page, I display the /ajax/select/dialog.php script:

 <script> function show() { $.ajax({ url: "../../ajax/select/dialog.php", cache: true, success: function(html){ $("#dtr").html(html); } }); } $(document).ready(function(){ show(); setInterval('show()',3000); }); </script> 

And memorization does not work, every 3 specified seconds the page jerks up.

Tell me how to be in this situation.

  • It seems that you need to handle the scroll not on the page being loaded, but on the page with Ajax. - LeonidMew Nov.
  • If #dtr itself has scrollbars, then you need to scroll it, otherwise the page on which the content is loaded. - LeonidMew
  • one
    Thanks you! It turned out) - Victor Muravyov

1 answer 1

If the page is loaded with Ajax, then in order to restore the scrolling position, you need to memorize / restore the position of the main page.
If the ajax is loaded into a div with overflow, i.e. scrolling, you probably need to memorize / restore the position of the div.