I've noticed that if you are on a page and you have scrolled down a good bit, if you refresh the page, most browsers will jump you back down to your position. Is there any way to prevent this?
I have looked at two options and neither are consistent across Webkit / Firefox.
window.scrollTo(0, 1);
$('html, body').animate({ scrollTop: 0 }, 0);
Any ideas?
You can check a google search result for an example.
I see no reason why this shouldn't work in all browsers, seems to work for me (with only one function for window.onload, use more and there could be problems) ?
To make it work when back button is clicked aswell, maybe something like this:
Just to update what @josetapadas said, for recent versions it would be better to use the
unload
function instead ofbeforeunload
this will beSomething like below works for me, create some item that can be focused and focus it to scroll page.
In this case it will scroll back to top after refresh the page.
Tested on latest IE/FF/Chrome.
Regards,
Ben
On Chrome, even if you force scrollTop to 0 it will jump afterwards after the first scroll event.
You should bind the scroll to this:
So the browser is tricked to believe that it was on the beginning before the refresh.
Works for me: