Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't force page to scroll to top on refresh on chrome

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

like image 764
Victor Ferreira Avatar asked May 03 '15 20:05

Victor Ferreira


People also ask

How do I make the page scrollTo the top on refresh?

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.

How do I scrollTo the top of a page in Chrome?

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.

Why is my webpage not scrolling?

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.

How do you maintain the scroll position?

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.


1 Answers

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

like image 163
xoxel Avatar answered Sep 21 '22 23:09

xoxel