How to get scrollTop of page afer browser scrolled such page?

Posted by Marco Demaio on Stack Overflow See other posts from Stack Overflow or by Marco Demaio
Published on 2010-03-29T18:24:06Z Indexed on 2010/03/29 18:33 UTC
Read the original article Hit count: 423

I know how to get the scrollTop of a page, I use this simple JS function (code copied around):

function GetScrolledTop() 
{
   //I never work in IE quirkmode, I always use DOCTYPE as 1st line, so I don't need to test for document.body.scrollTop
   return self['pageYOffset'] || document.documentElement.scrollTop; 
}

This works and my problem is the following: I tried to add it in the page onload event

<body onload="alert(GetScrolledTop());">

On page load I get ZERO (which make sense), but the problem is that I get ZERO even if I scroll the page and then reload it without touching the scrollbar.

It seems like the browser does:

  1. loads page
  2. calls my GetScrolledTop() (so obviously shows ZERO)
  3. then scrolls the page to where it was before.

Do you know how to get the scolledTop after the step 3? I mean how to get the scrolledTop AFTER the browser scrolled the page? (maybe without using a timer)

Thanks!

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about scrolltop