How to scroll to top of page with JavaScript/jQuery?

Is there a way to control browser scrolling with JavaScript/jQuery?

When I scroll my page half way down, then trigger a reload, I want the page to go pack to the top, but instead it tries to find the last scroll position. So I did this:

$('document').ready(function() {
   $(window).scrollTop(0);
});

But no luck.

EDIT:

So both your answers worked when I call them after the page loads-Thanks. However, if I just do a refresh on the page, looks like the browser calculates and scrolls to its old scroll position AFTER the .ready event (I tested the body onload() function too).

So the follow up is, is there a way to PREVENT the browser scrolling to its past position, or to re-scroll to the top AFTER it does its thing?


Cross-browser, pure JavaScript solution:

document.body.scrollTop = document.documentElement.scrollTop = 0;

You almost got it - you need to set the scrollTop on body, not window:

$(function() {
   $('body').scrollTop(0);
});

EDIT:

Maybe you can add a blank anchor to the top of the page:

$(function() {
   $('<a name="top"/>').insertBefore($('body').children().eq(0));
   window.location.hash = 'top';
});

Wow, I'm 9 years late to this question. Here you go:

Add this code to your onload.

// This prevents the page from scrolling down to where it was previously.
if ('scrollRestoration' in history) {
    history.scrollRestoration = 'manual';
}
// This is needed if the user scrolls down during page load and you want to make sure the page is scrolled to the top once it's fully loaded. This has Cross-browser support.
window.scrollTo(0,0);

To run it on window load just put it wrap it like this (assumes you have JQuery referenced)

$(function() {
  // put the code here
});

history.scrollRestoration Browser support:

Chrome: supported (since 46)

Firefox: supported (since 46)

Edge: supported (since 79)

IE: not supported

Opera: supported (since 33)

Safari: supported

For IE if you want to re-scroll to the top AFTER it autoscrolls down then this worked for me:

var isIE11 = !!window.MSInputMethodContext && !!document.documentMode;
if(isIE11) {
    setTimeout(function(){ window.scrollTo(0, 0); }, 300);  // adjust time according to your page. The better solution would be to possibly tie into some event and trigger once the autoscrolling goes to the top.
} 

Is there a way to PREVENT the browser scrolling to its past position, or to re-scroll to the top AFTER it does its thing?

The following jquery solution works for me:

$(window).unload(function() {
    $('body').scrollTop(0);
});

UPDATE

Going to top of the page with a scroll effect is a bit more easier in javascript now with:

https://developer.mozilla.org/en-US/docs/Web/API/Window/scroll

There are 2 ways to use scroll API.

This is the method I recommend. Using an option object:

window.scroll(options)

This is a better option since you can define a behavior prop which applies a built-in easing animation.

window.scroll({
 top: 0, 
 left: 0, 
 behavior: 'smooth' 
});

The other method is to use an x and y coordinates.

window.scroll(x-coord, y-coord)

x-coord - is the pixel along the horizontal axis of the document that you want displayed in the upper left.

y-coord - is the pixel along the vertical axis of the document that you want displayed in the upper left.


OLD ANSWER DO NOT USE

This is our vanilla javascript implementation. It has a simple easing effect so that the user doesn't get shocked after clicking the To Top button.

Its very small and gets even smaller when minified. Devs looking for an alternative to the jquery method but want the same results can try this.

JS

document.querySelector("#to-top").addEventListener("click", function(){

    var toTopInterval = setInterval(function(){

        var supportedScrollTop = document.body.scrollTop > 0 ? document.body : document.documentElement;

        if (supportedScrollTop.scrollTop > 0) {
            supportedScrollTop.scrollTop = supportedScrollTop.scrollTop - 50;
        }

        if (supportedScrollTop.scrollTop < 1) {
            clearInterval(toTopInterval);
        }

    }, 10);

},false);

HTML

<button id="to-top">To Top</button>

Cheers!