-5

$(document).ready(function(){ $("#addmedic").click(function(){ sessionStorage.setItem("pos", $("div").scrollTop()); }); }); $(document).ready(function(){ if (sessionStorage.getItem("pos") != "undefined") { $("div").scrollTop(sessionStorage.getItem("pos")); } });

I have used Javascript scrollTo and scrollBy function. But its not working

for me . I want to store the position of scroll so that I can get after loading the web page too.

2 Answers2

1

Store scroll position in cookie before leaving page, then in case of page redirect use that cookie for setting the scroll position.

Tarun Khurana
  • 887
  • 8
  • 9
1

at scroll you can take id of the element and store it in a variable. than Use focus() for that with ID. It will work for you. handle null for that variable before you make focus on it.

this is good example

$(window).scroll(function() {
  sessionStorage.scrollTop = $(this).scrollTop();
});

$(document).ready(function() {
  if (sessionStorage.scrollTop != "undefined") {
    $(window).scrollTop(sessionStorage.scrollTop);
  }
});
  • i have tried with focus it going on that position means it is focusing there but after that again scroll come on top. – rishabhkeshari123 Oct 14 '16 at 09:05
  • thanks for ur reply – rishabhkeshari123 Oct 14 '16 at 09:05
  • Oh man than stop scroll up by e.preventdefault –  Oct 14 '16 at 12:30
  • – rishabhkeshari123 Oct 14 '16 at 13:15
  • $(document).ready(function(){ $("#addmedic").click(function(){ sessionStorage.setItem("pos", $("div").scrollTop()); }); }); $(document).ready(function(){ if (sessionStorage.getItem("pos") != "undefined") { $("div").scrollTop(sessionStorage.getItem("pos")); } }); – rishabhkeshari123 Oct 14 '16 at 13:17
  • thanq Rishu sir , Please help me like this i am storing and i am getting after loading . or during loading you can say. but scroll behaving its default nature and its going on top. – rishabhkeshari123 Oct 14 '16 at 13:20