Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 528
Meow Avatar asked May 27 '11 06:05

Meow


1 Answers

The correct way to check if you reached the bottom of the page is this:

  1. Get document.body.clientHeight = the height of the ACTUAL screen shown
  2. Get document.body.offsetHeight or document.body.scrollHeight = the height of the entire page shown
  3. Check if document.body.scrollTop = document.body.scrollHeight - document.body.clientHeight

If 3 is true, you reached the bottom of the page

like image 189
Miki Berkovich Avatar answered Nov 10 '22 06:11

Miki Berkovich