Javascript: Maintaining Page Scroll Position
This is a small example how to maintain scroll position of the page/div during refresh with the help of several lines of javascript code:
< html>
< title>< /title>
< head>
< script language="javascript">
// function saves scroll position
function fScroll(val)
{
var hidScroll = document.getElementById('hidScroll');
hidScroll.value = val.scrollTop;
}
// function moves scroll position to saved value
function fScrollMove(what)
{
var hidScroll = document.getElementById('hidScroll');
document.getElementById(what).scrollTop = hidScroll.value;
}
< /script>
< /head>
< body onload="fScrollMove('div_scroll');" onunload="document.forms(0).submit()";>
< form>
< input type="text" id="hidScroll" name="a">< /br>
< div id="div_scroll" onscroll="fScroll(this);"
style="overflow:auto;height:100px;width:100px;">
.. VERY LONG TEXT GOES HERE
< /div>
< /form>
< /body>
< /html>
The example is supposed to be cross-browser compatible. Enjoy :)
Thursday, December 11, 2003 4:56 AM