i'm trying to force the page to scroll to top on refresh with something similar to this
$(window).scrollTop(0);
But it seems to happen before the automatic scroll down on refresh. My script runs and then the browser restores its last position.
I'm running the code in several places like this
$(function(){
$(window).scrollTop(0);
});
$(window).scrollTop(0);
$(document).ready(function(){
$(window).scrollTop(0);
});
$(window).on('load',function(){
$(window).scrollTop(0);
});
BUt the same happens all the time. Do I have to put this code somewhere else? Load the JS in a specific part of the HTML? Is there anything else on pure JS or jQuery that could help me with this issue?
@Edit
I tried with $(html, body).scrollTo(0)
and couldnt make it work
I tried without jQuery and nothing happened
window.scrollTo(0,0)
I disabled everything I had written with Javascript and put only this piece of code and nothing happens.
@edit2
I've had this problem before and I'm sure i 'solved' this with a setTimeout
, as suggested by @raam86. The problem is that I do some math when page loads and this must be done before the user starts scrolling up and down
onbeforeunload property to a function that calls window. scrollTo with 0 and 0 to scroll to the top. Now when we refresh the page, we scroll to the top of the page.
To jump to the bottom of a page, hit Command-down arrow. You can then return to the top of the page with Command-up arrow. On Windows, hit the Home and End keys to go to the top and bottom of a page, respectively.
Your website might not be scrolling if there is a problem with your browser (Google Chrome, Safari, etc.), website code, or your computer hardware. You can investigate & fix the issue by restarting the browser, looking at the site code, deleting cookies, extensions, and/or caches.
To make sure a scrolling Artboard stays in position when you click on a prototype Link, select the Link you're working with and enable the Maintain scroll position after click option in the PROTOTYPE tab of the Inspector.
Correct answer is:
window.onbeforeunload = function () {
window.scrollTo(0,0);
};
Because Chrome and others browsers "remember" the last scroll position before unloading, so if you set the value to 0,0 just before the unload of your page they will remember of 0,0 and won't scroll back to where the scrollbar was :)
Just past that on all the page's controllers and they will scroll to top on reload/refresh/others
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With