Weird Thoughts From Eric's Head

Categories : All | AJAX | BUSINESS | PERSONAL | PROGRAMMING | BOOK REVIEW

Remember a Div's Scroll Position
A person asked the question on how to remember a div's scroll position on the page when the comes back to the page. It is rather easy to do and it involves a session cookie to remember the position. Yeah, the cookie part could be done better so do not comment on that! I went the easy route!

<html>
  <head>
    <title>Fun Scroll</title>
    <style type="text/css">
      #divTest{width:150px;height:200px;overflow:auto}
    </style>
    <script type="text/javascript">
      window.onload = function(){
        var strCook = document.cookie;
        if(strCook.indexOf("!~")!=0){
          var intS = strCook.indexOf("!~");
          var intE = strCook.indexOf("~!");
          var strPos = strCook.substring(intS+2,intE);
          document.getElementById("divTest").scrollTop = strPos;
        }
      }
      function SetDivPosition(){
        var intY = document.getElementById("divTest").scrollTop;
        document.title = intY;
        document.cookie = "yPos=!~" + intY + "~!";
      }
    </script>
  </head>
  <body>
    <div id="divTest" onscroll="SetDivPosition()">
      1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>
      1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>
      1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>
      1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>
      ERIC<br/>
      1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>
      1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>
      1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>
      1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>
    </div>
  </body>
</html>
Hope it helps,

Eric Pascarello
Moderator of HTML/JavaScript at www.JavaRanch.com
Author of: JavaScript: Your Visual Blueprint for Dynamic Web Pages
Co-Author of Ajax In Action


This tip is absolutely top class!
Thanku so much for a very very useful script.
Thanks for the tip!
awesome tip for retaining scroll position when using a datagrid!
That's helpful. But isn't a cookie very slow? Doesn't every change in the scroll position imply writing to disk? How would you do it otherwise? Hidden fields?
You could write it to a hidden field on the page or the cookie. The IO to the cookie can be slow, I have not seen any problems with this script yet, could do something with a timer in it to limit rewrites which I have in the works. With that said I actually have plans to update this script a little more, should be up next week.

Eric
Thanks a lot for the tip. I spent a lot of time trying to find out this solution for the datagrid/div remember position. It works great with the cookie, however I could not get it to work with the hidden field. Is there some gotcha involved with that approach?
Thanks a lot Eric for the help.. got it working with a hidden field.. with ASp.net 2.0.. needed to make the hidden field run at server for it to actually hold on to the value.. strange but works great..
Thanks for your code.
Thanks very much for the tip!!!! Matteo
Thanks . Small code big solution.
Thanks for this very usefull script! It works perfectly.
Thank you! Thank you! Thank you! You just saved me so many late nights! Again, thank you!
Read so many long, convoluted solutions. This is the only one that really worked. And nice and compact to boot. Thank you very much.
Hi I needed to make a scrollable div, and i did it, but i have the following problem: when i try to run my resulting page in Internet Explorer, it doesn't work. No matter what value i put in my cookie, when i load it, and try to set the value to div's scrollTop position, it doesn't put's me a value larger than 20 and in some cases, 40. I mention that in Firefox, it's running smoothly, whithout any kind of problems. I would appreciate any kind of help, here, or on my e-mail address, as i really need to make it work with Internet Explorer If someone can help, please, contact me (johnny_r3d@yahoo.com) Thank you
Hello, The script works fine in Firefox browser, but I guess there is a bug in IE 6, because if having a scroll > 28, the page will be reloaded with this value as scroll.
Actually we wrote our own code, and while searching for clues to this problem I found this nice script, which could have saved us some time.
Anyway, I saw that a way to solve this problem is to assign the scrollTop property twice. So instead of
document.getElementById("divTest").scrollTop = strPos;
I have:
document.getElementById("divTest").scrollTop = strPos;
document.getElementById("divTest").scrollTop = strPos;
and it seems to work. I hope I've been of some help.
Very helpfull, thank you. I tested on mozzila firefox 1.5.0.3 and it worked, but it doesn't worked on IE 6.0. is there something I must do with the IE setting? sorry for my poor english
Hi, This seems very cool! But can you guys make it work horizontaly? Thanx!
Thanks. Saved my day.
This works great for me in firefox. I cannot get it to work in IE 6 or 7. Has anyone solved this yet? Also to minimize writes to disk I believe there is a window.unload event that you might be able to tie into so you only have to write to the cookie once. That being said I haven't seen any slowness using the script on my local dev machine and it isn't server strength.
The cookie part could have been done better you know
Excellent!!! But, how can we adapt this idea to a panel. I have my grids in panels because there are 3 of them on the page, one on the left and 2 on the right that feed off the selection made in the first one. The search text box searches for a match in the first grid and selects the correct row but if it is way down the grid I cannot get it to programmaticaly scroll down to the selected row.
hey, I tried the given script on to an asp:Panel and it does the thing alright. The only problem is that it flickers too much because the scroll first goes to the top and then comes down. Any suggestions for it?? Thanks, Ashwani
Very, very usefull, thank you Eric !
Thank You. Indeed a Very nice and excellent Solution to retain scroll position on post back. Thanks Again.


Add a comment

Title
Body
HTML : b, i, blockquote, br, p, pre, a href="", ul, ol, li
Math Quiz 1 + 6 = (Helps stop blog spam)
Name
E-mail address
Website
Remember me Yes  No 

E-mail addresses are not publicly displayed, so please only leave your e-mail address if you would like to be notified when new comments are added to this blog entry (you can opt-out later).

TrackBack to http://radio.javaranch.com/pascarello/addTrackBack.action?entry=1121709316718