SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    Maryland
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to do proper object detection

    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:

    Code:
    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:

    Code:
    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.

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have a look at the following (from the X library).
    Code:
    function xScrollTop(e) {
      var offset=0;
      if (!(e=xGetElementById(e))) {
        if(xDef(window.pageYOffset)) offset=window.pageYOffset;
        else if(document.documentElement && document.documentElement.scrollTop) offset=document.documentElement.scrollTop;
        else if(document.body && xDef(document.body.scrollTop)) offset=document.body.scrollTop;
      }
      else { if (xNum(e.scrollTop)) offset = e.scrollTop; }
      return offset;
    }
    
    function xDef() {
      for(var i=0; i<arguments.length; ++i){if(typeof(arguments[i])=='undefined') return false;}
      return true;
    }
    
    function xNum(n) {
      return typeof(n)=='number';
    }

  3. #3
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Notice this expression:

    xDef(document.body.scrollTop)

    which is equivalent to:

    typeof(document.body.scrollTop) != 'undefined'

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    Maryland
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Mike,

    Have you tested your code under IE 5.5? If you don't have IE 5.5, then I think you can also use IE 5 or IE 6 in quirks mode.

    Initialize your offset variable to 5 (or anything besides 0), because the code should still work that way. But I think if you run the code under IE 5.5 (or probably even IE 5 or IE 6 in quirks mode), you will notice that it does not return the correct value.

  5. #5
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure what you mean. Why initialize it to non-zero?

    The answer to your original question is in my second post.

  6. #6
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    Maryland
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Checking for typeof(document.body.scrollTop) != 'undefined' and also typeof(document.documentElement.scrollTop) != 'undefined' wont work, because both of those expressions evaluate to true in both IE 5.5 and IE 6, since document.body.scrollTop and document.documentElement.scrollTop are both defined in IE 5.5 and IE 6.

    I'm just saying that initializing it to zero will make the code work by accident (assuming the page hasn't scrolled vertically). Initializing it to non-zero will show you that the code doesn't work when the page hasn't scrolled. And it really doesn't matter what it is initialized to, because its value will be replaced by the later assignment statements.

    Actually, you don't even have to change your code. What value is being returned in IE 5.5 when the page has scrolled?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •