Maintain scroll position on postback

Posted written by Paul Seal on November 05, 2015 Tips

If you want your site to maintain the current scroll position when the page reloads or when you go to another page, this post should help you.

It is ideal for paging, or for sites where you don't want to submit a form using Ajax, but you still want it to stay in position.

These 8 lines of code work a treat. Just make sure you reference jQuery and jquery.cookie.js before it.

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
 <script src="/scripts/jquery.cookie.js"></script>

 <script type="text/javascript">
     $(function () {
     var top = parseInt($.cookie("top"));
     if (top) $(document).scrollTop(top);
         $(document).scroll(function () {
             var top = $(document).scrollTop();
             $.cookie("top", top);
         })
     });
 </script>

Please feel free to comment and share with you friends/colleagues.

I originally found this code here