Alex Rivera | Logout

How to determine if vertical scroll bar has reached the bottom of the web page?

Asked 2011-05-27T06:38:41.017
10

The same question is answered in jQUery but I'm looking for solution without jQuery. How do you know the scroll bar has reached bottom of a page

I would like to know how I can determine whether vertical scrollbar has reached the bottom of the web page.

I am using Firefox3.6

I wrote simple Javascript loop to scroll down by 200 pixel and when the scroll bar reached the bottom of the page, I want to stop the loop.

The problem is scrollHeight() is returning 1989.

And inside loop scrollTop is incremented by 200 per iteration.

200 ==> 400 ==> 600 .... 1715

And from 1715, it won't increment so this loop continues forever.

Looks like scrollHeight() and scrollTop() is not right way to compare in order to determine the actual position of scrollbar? How can I know when the loop should stop?

code:

var curWindow = selenium.browserbot.getCurrentWindow();
var scrollTop = curWindow.document.body.scrollTop;
alert('scrollHeight==>' + curWindow.document.body.scrollHeight);

    while(curWindow.document.body.scrollHeight > curWindow.document.body.scrollTop) {
      scrollTop = curWindow.document.body.scrollTop;
      if(scrollTop == 0) { 
        if(window.pageYOffset) { //firefox
          alert('firefox'); 
          scrollTop = window.pageYOffset; 
        } 
        else { //IE
          alert('IE'); 
          scrollTop = (curWindow.document.body.parentElement) ? curWindow.document.body.parentElement.scrollTop : 0; 
        } 
      } //end outer if
      alert('current scrollTop ==> ' + scrollTop);
      alert('take a shot here'); 
      selenium.browserbot.getCurrentWindow().scrollBy(0,200);

    } //end while
Edit
Report

1 Answer

4
function scrollHandler(theElement){
   if((theElement.scrollHeight - theElement.scrollTop) + "px" == theElement.style.height)
       alert("Bottom");
}

For the HTML element (like div) add the event -- onscroll='scrollHandler(this)'.

answered 2012-11-23T09:48:45.440

Your Answer