473,322 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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:

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.

Jul 20 '05 #1
2 1214


de*******@no.spam.com 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/

Jul 20 '05 #2
Thanks for the reply, Martin.

Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: news.onetel.net.uk | last post by:
I and my friend Karl have spent literally all day trying to find out what is causing my error but we are zapped of any further functionality :) I have a form that adds news records. You select...
23
by: Andrew Thompson | last post by:
This is intended to fix a long standing (cough)Java(cough) problem with which I'm sure some of you will be familiar. Sun recommends using the <OBJECT>/<EMBED> elements to cater for Java...
3
by: roberts.noah | last post by:
Yesturday I chased down a memory leak being reported by our leak detection library. It lead me to question this line of code: str_instance = std::string(); I changed it to...
79
by: VK | last post by:
I wandering about the common proctice of some UA's producers to spoof the UA string to pretend to be another browser (most often IE). Shouldn't it be considered as a trademark violation of the...
4
by: zoookapi | last post by:
I'm attempting to dynamically add a link so I can take a mouseover action on it and am having problems in Safari (this works fine in Firefox and IE (Windows version of IE)). When I add the img...
0
by: freshman | last post by:
# test code: http://pyode.sourceforge.net/tutorials/tutorial3.html # #i want selecting object in pyode test code. # what's wrong? # under modify code # see selectObject() function # pyODE...
9
by: Cylix | last post by:
The following example are going to create an object to store the client Information, I would like to new the object to init all the properties by function: setProperty() Can I set the value to...
9
by: uidzer0 | last post by:
Hey everyone, Taken the following code; is there a "proper" or dynamic way to allocate the length of line? #include <stdio.h> #include <errno.h> int main(int argc, char **argv) { FILE *fp;
0
by: origami.takarana | last post by:
Intrusion Detection Strategies ----------------------------------- Until now, we’ve primarily discussed monitoring in how it relates to intrusion detection, but there’s more to an overall...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.