0

I've scrolled page downto the middle and then reload page (F5). After that scroll still in the middle. It's normal. My task is to set scrollY = 0 after page loaded.

If I set document.documentElement.scrollTop = 0 (document.body.scrollTop = 0 or window.pageXOffset = 0 and within or out of $(document).ready(...) or $(window).load(...) ) page is not scrolled to the top.

I have this function:

function getScrollXY() {
    var x = 0, y = 0;
    if( typeof( window.pageYOffset ) == 'number' ) {
        // Netscape
        x = window.pageXOffset;
        y = window.pageYOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        // DOM
        x = document.body.scrollLeft;
        y = document.body.scrollTop;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        // IE6 standards compliant mode
        x = document.documentElement.scrollLeft;
        y = document.documentElement.scrollTop;
    }
    return [x, y];
}

It always return x=0 even if page is scrolled after page was loaded.

So how to set scroll to top after page loaded?

The code on jsfiddle: http://jsfiddle.net/g6w9r/1/

The frame from jsfiddle to see the result: http://fiddle.jshell.net/g6w9r/1/show/

Anthony
  • 3,218
  • 3
  • 43
  • 73

1 Answers1

0
window.scrollTo(x-coord, y-coord);
CaribouCode
  • 13,998
  • 28
  • 102
  • 174