Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > How to do proper object detection

Reply
Thread Tools

How to do proper object detection

 
 
delerious@no.spam.com
Guest
Posts: n/a
 
      12-16-2003
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.

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      12-16-2003


http://www.velocityreviews.com/forums/(E-Mail Removed) 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/

 
Reply With Quote
 
 
 
 
delerious@no.spam.com
Guest
Posts: n/a
 
      12-16-2003
Thanks for the reply, Martin.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
OpenCV Object Detection T Python 0 07-15-2011 07:37 PM
Cycle detection and object memory usage? Jim Kleckner Python 1 05-21-2007 03:45 AM
Camera software with object detection capability? Bert Hyman Digital Photography 3 08-30-2006 07:45 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM



Advertisments