There are answers all over the net to this problem, and personally none of them worked, as for me Firefox would attempt to recover the previous scroll position (wrongly), trigger the window.scroll
event, which would overwrite my hidden field with its wrong position, which my scrollTo
would then read. (I have gridviews coming from postbacks followed by automatic collapsing of some rows.)
So here's yet another solution to this problem - I decided I only want to recover the scroll position after a submit, not a refresh, so this was adequate:
ASPX page:
<form runat="server" onsubmit="$('#hfScroll').val($(window).scrollTop()); return true;">
<input type="hidden" id="hfScroll" value="0" />
Javascript:
function restoreScroll()
{
var position = parseInt($('#hfScroll').val());
if (!isNaN(position)) {
$(document).scrollTop(position);
}
};
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_pageLoaded(restoreScroll);
For some reason, on browser refresh my hidden input is not reset to zero, so this does behave odd sometimes. I would love to know what's doing this, I think it is Firefox as it doesn't happen on IE, but life's too short [he says...having downloaded half the internet and spent hours on this..].