I have a page operation that uses something like:
$(\'#thetable tbody\').replaceWith(newtbody);
in an ajax callback. Sometimes, if the user
var position= $(window).scrollTop();
//some things here
$(window).scrollTop(position);
It worked for me in both IE8 and FF.
Try to use .html()
instead of .replaceWith()
:
$('#thetable tbody').html( newtbody.html() );
.
I don't know about possible performance bottlenecks here, but it did the trick for me when I was trying to preserve the scroll position of a div, and it seems to do well with the scroll position of the entire page as well.
Be sure to use return false;
to terminate your function(){}
construct.
The event trigger may be trying to execute the default action of the target DOM element i.e., < a href="" >;
We had the exact same problem, and the following code works great...
var scroll = $(window).scrollTop();
// yada
$("html").scrollTop(scroll);
You could periodically check the div
outerHeight
, and set the scrollTop
property once the div
rendering has crossed the scroll value.
var g_Interval ;
var g_ScrollTop ;
function checkAndAdjustScroll() {
var height=$('#content').outerHeight() ;
if(height>g_ScrollTop) {
$(window).scrollTop(g_ScrollTop) ;
clearInterval(g_Interval) ;
}
}
And whenever you update your div with AJAX content, do this
g_ScrollTop=$(window).scrollTop() ;
g_Interval=setInterval(checkAndAdjustScroll, 100) ;
You might need to make adjustments based on the offset of your div
from the top of the page
Are u use aspx? if it's aspx:
$(document).ready(function () {
//------------ scroll-------------
$('body').scrollTop(document.getElementById('<%=hdScroll.ClientID%>').value);
$(window).scroll(function () {
$("#<%= hdScroll.ClientID %>").val($(window).scrollTop());
//alert(document.getElementById('<%=hdScroll.ClientID%>').value);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<asp:HiddenField Id="hdScroll" runat="server"></asp:HiddenField>