Restoring page scroll position with jQuery
<p>I have a page operation that uses something like:</p>

<pre><code>$('#thetable tbody').replaceWith(newtbody);
</code></pre>

<p>in an ajax callback. Sometimes, if the user had scrolled the page down, this operation has the understandable side effect of scrolling the page back up. But the replacement appears seamless to the user so it's a bit annoying to have to scroll back down again. And since the <code>newtbody</code> normally has the same vertical height as the one it replaced, we should be able to make the script do it instead.</p>

<p>Now, since I found that executing:</p>

<pre><code>$('body').scrollTop(300);
</code></pre>

<p>from the JS debugger console does what I hoped it would, I thought the simple remedy would be:</p>

<pre><code>var scrollsave = $('body').scrollTop();
$('#thetable tbody').replaceWith(newtbody);
$('body').scrollTop(scrollsave);
</code></pre>

<p>but no joy. I haven't resorted to <a href="http://flesler.blogspot.com/2007/10/jqueryscrollto.html" rel="nofollow">jQuery.ScrollTo</a> yet.</p>
