Can't get the page to scroll up to refresh chrome
I am trying to get the page to scroll to the top in order to update something like this
$(window).scrollTop(0);
But this happens before the automatic scrolling down on the update occurs. My script runs and then the browser restores its last position.
I am running the code in several places like
$(function(){
$(window).scrollTop(0);
});
$(window).scrollTop(0);
$(document).ready(function(){
$(window).scrollTop(0);
});
$(window).on('load',function(){
$(window).scrollTop(0);
});
BUt the same thing happens all the time. Should I put this code somewhere else? Load JS into a specific part of HTML? Is there anything else in pure JS or jQuery that might help me with this problem?
@Edit
I tried with $(html, body).scrollTo(0)
and couldn't get it to work
I tried without jQuery and nothing happened
window.scrollTo(0,0)
I turned off everything I wrote in Javascript, and put only this piece of code, and nothing happened.
@ edit2
I've had this problem before and I'm pretty sure I "solved" it with the help setTimeout
as @ raam86 suggested. The problem is that I am doing some math on page load and this needs to be done before the user starts scrolling up and down
source to share
Correct answer:
window.onbeforeunload = function () {
window.scrollTo(0,0);
};
Because Chrome and other browsers "remember" the last scroll position before unloading, so if you set a value to 0.0 just before unloading your page, they will remember 0.0 and won't scroll back to where the scrollbar was :)
More recently, on all page controllers and they will scroll up to reload / refresh / others
source to share