Anatoly Lubarsky

Logo
MSSQL, .NET, Design. Life and Music

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 :)


Related Posts:

Thursday, December 11, 2003 4:56 AM

Comments

# re: Javascript: Maintaining Page Scroll Position
Hello,

I tried to change this function to use it for more than one scrollbar in one page. I did not get this to work.
What to to to get this one work for more than one scrollbar ??

Thanks

1/14/2008 7:12 PM by Marcel

# re: Javascript: Maintaining Page Scroll Position
scroll issue

2/22/2008 6:18 PM by ankur

If your feedback doesn't appear right away, please be patient as it may take a few minutes to publish.

Post a Comment

Protected by CAPTCHAEnter the code you see
Name (*)  
E-mail (*)  
Url
Remember

Comment (*)