IE: scrollTop Not Working As Expected


I banged my head on this one for a couple of days.  Still got a defect out of the deal, but finally got it working.  I had an ajax tabcontainer with several tabs.  In some of these tabs was GridView.  I had overflow set on a DIV that the GridView was placed in so that I would get vertical scrollbars within the page.  The issue was that I needed to keep the position of the DIV scroll on a postback.  Should not have been an issue since I have the exact same scenario on another page within the same website which work perfectly.  But for some reason, using the same code, it would not work on this particular page.  Both pages use MasterPages, tabcontainers, and a little javascript and cookies to save the scroll position.

When debugging the issue, I would find that it would save and get the correct scroll position from the cookie; however, when setting the scrollTop:

document.getElementById('myDiv').scrollTop = scrollPosition;

It would not set.  The DIV’s scrollTop would be 0 before and after the statement above even after verifying that scrollPosition had an actual value!

I will also mention that the grid did have a decent about of data.  Actually, enough to make the page size about 3.5 MBs by the time HTML was rendered…almost 1 MB taken up by ViewState alone!  Because of AJAX and the amount of data in the grid, there was a noticeable amount of lag when using the page…not a lot, but noticeable.

Work Around:

I started to wonder if the above javascript (which ran on window.onload) may be running too quickly…or rather, ran before IE could finish with the rest of the AJAX/javascript code on the page.  So I tried this line instead of the one above:

setTimeout(function() { document.getElementById('myDiv').scrollTop = scrollPosition; }, 250);

So basically, I wanted to wait 250 milliseconds before setting the scrollTop.  Much to my surprise, this worked!  By this point I was too exhausted to keep knocking down the delay to see how low it could go and still work.  I couldn’t really notice the 250 delay in setting the scroll position, so that is where I ended.

Your mileage may vary, but in case you have a similar issue…try a small delay when setting the scrollTop before attempting to re-engineer the whole page.

, , ,

  1. No comments yet.
(will not be published)