Essentially, the script listed below is the one that will save the scroll position constantly to hidden fields. The rate that the scroll position is saved is 100 times per second to ensure maximum resolution of where the user is. The script is listed below:
function SaveScrollPositions() {
document.forms[0].StaticPostBackScrollVerticalPosition.value =
(navigator.appName == 'Netscape') ? document.pageYOffset : document.body.scrollTop;
document.forms[0].StaticPostBackScrollHorizontalPosition.value =
(navigator.appName == 'Netscape') ? document.pageXOffset : document.body.scrollLeft;
setTimeout('SaveScrollPositions()', 10);
}
SaveScrollPositions();
And, when the page has been posted back to, it then renders the following JavaScript:
function RestoreScrollPosition() {
scrollTo(0, 400); // determined during run-time
}
window.onload = RestoreScrollPosition;
What is noteworthy on the above JavaScript is that the RestoreScrollPosition method is only called once the page has loaded. The JavaScript waits for the page to load completely so we could overcome a bug in Internet Explorer when the script attempts to scroll the page (via the scrollTo client method). If another JavaScript relies on this type of listening, either StaticScrollBackPosition control will not work or the other JavaScript block(s) will not work correctly – so be warned that this will be the only known bug.
However, I did try to soften the blow and told it to attach to the window's load event and not the document's load event. Therefore, if you have some HTML, as shown below, this short-fall (bug) will not effect you at all:
<body onLoad="...">
<!-- some code -->
</body>