How to do proper object detection

Discussion in 'Javascript' started by delerious@no.spam.com, Dec 16, 2003.

  1. Guest

    I've read that IE 5/5.5 stores elements such as scrollLeft and scrollTop in
    the document.body object, and IE6 stores those elements in the
    document.documentElement object.

    I wrote up the following javascript function:

    function getScrollTop() {
    var s = 0;
    if (document.documentElement && typeof(document.documentElement.scrollTop)
    != 'undefined') {
    s = document.documentElement.scrollTop;
    }
    else if (document.body && typeof(document.body.scrollTop) != 'undefined') {
    s = document.body.scrollTop;
    }
    return s;
    }

    But that didn't work as document.documentElement.scrollTop is actually defined
    in IE 5.5 (it always returns 0 though).

    So I rewrote it like this:

    function getScrollTop() {
    var s = 0;
    if (document.documentElement && document.documentElement.scrollTop) {
    s = document.documentElement.scrollTop;
    }
    else if (document.body && document.body.scrollTop) {
    s = document.body.scrollTop;
    }
    return s;
    }

    That works, but if the page hasn't scrolled at all, then it is only working by
    accident. If the page hasn't scrolled at all, the code within both the if
    clause and the else-if clause are not executed, because the scrollTop has a
    number value of 0, and javascript treats a 0 number value as false. So the
    function isn't really working correctly, but it is returning the correct value
    since the s variable is initialized to 0. If the s variable were initialized
    to 10, then the function would return the wrong value.

    I am wondering how I can get IE to go into the proper if/else-if clause.
    , Dec 16, 2003
    #1
    1. Advertising

  2. wrote:

    > I've read that IE 5/5.5 stores elements such as scrollLeft and scrollTop in
    > the document.body object, and IE6 stores those elements in the
    > document.documentElement object.


    That is not correct, the properties exist on both element objects, only
    if you want to know the scroll position of the viewport then you need to
    check the right element.
    That is document.body with IE5/5.5 in any case but with IE6 it depends
    on document.compatMode whether document.body or document.documentElement
    is the right element to look at:
    var scrollLeft;
    if (typeof document.compatMode != 'undefined' &&
    document.compatMode != 'BackCompat')
    scrollLeft = document.documentElement.scrollLeft;
    else
    scrollLeft = document.body.scrollLeft;
    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Dec 16, 2003
    #2
    1. Advertising

  3. Guest

    Thanks for the reply, Martin.
    , Dec 16, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wolfgang
    Replies:
    7
    Views:
    434
    Dale King
    Jan 23, 2004
  2. Jim Kleckner

    Cycle detection and object memory usage?

    Jim Kleckner, May 21, 2007, in forum: Python
    Replies:
    1
    Views:
    264
    John Nagle
    May 21, 2007
  3. T

    OpenCV Object Detection

    T, Jul 15, 2011, in forum: Python
    Replies:
    0
    Views:
    689
  4. James
    Replies:
    2
    Views:
    118
    David Mark
    Apr 20, 2011
  5. Replies:
    1
    Views:
    68
    Giorgos Tzampanakis
    Jan 24, 2014
Loading...

Share This Page