Get Scrollbar Position

R

Richard Cornford

yOff = parseInt(MenuObject.style.height,10) -
(document.all?document.body.scrollTop:window.pageYOffset);

All decision making by object inference is misguided, and in all cases
it is preferable to read scroll values from the global pageX/YOffset
properties as that removes the need to worry about the
root-element/document.compatMode problem in the vast majority of
browsers.

Will be faster then without replacing
document.getElementById('contenxtmenu')
by
MenuObject
?

getElementById potentially searches the entire DOM for the requested
element. Even if optimised to look only in some sort of collection of
IDed elements repeating that process for each reference must be less
efficient than doing it once and holding on to the result of the first
look-up.
Does the same hold for
document.getElementById('contenxtmenu').style
?
Because that's the only object I'll use of the
document.getElementById('contenxtmenu') element.

If the element's style object is only the object you are interested in
then that is the object to which it makes most sense to hold an
appropriately scoped reference rather than repeatedly re-resolving it.
And is it smart to replace:
document.body.scrollLeft
by
document.body.scrollLeft + document.documentElement.scrollLeft

The root-element/document.compatMode problem is not addressed by this
code.

Richard.
 
D

DJ WIce

: <snip>
: >yOff = parseInt(MenuObject.style.height,10) -
: >(document.all?document.body.scrollTop:window.pageYOffset);
:
: All decision making by object inference is misguided, and in all cases
: it is preferable to read scroll values from the global pageX/YOffset
: properties as that removes the need to worry about the
: root-element/document.compatMode problem in the vast majority of
: browsers.

How can I address those?
I understand that Mozilla uses: window.pageYOffset and Netscape also from
version 3.
But MSIE does not, and in not strick mode it returns 0 for
document.documentElement.scrollTop.
If I'm correct you mean I should not need to check for document.all and I
should not adres the pageYOffset via the .body part.
I don't know how to adres it then (I could not find an example of that via
google).

Or should it be like this:

IEroot = (document.compatMode!="BackCompat")? document.documentElement :
document.body;

And then
document.all ? IEroot.scrollTop : window.pageYOffset;
?


: >And is it smart to replace:
: > document.body.scrollLeft
: >by
: > document.body.scrollLeft + document.documentElement.scrollLeft
:
: The root-element/document.compatMode problem is not addressed by this
: code.
So the answer is yes? (sorry for asking confirmation, I'm not sure that I do
understand the answer correctly).

Wouter
 
R

Richard Cornford

How can I address those?
I understand that Mozilla uses: window.pageYOffset and
Netscape also from version 3.

Along with many other browsers.
But MSIE does not, and in not strick mode it returns 0 for
document.documentElement.scrollTop.
If I'm correct you mean I should not need to check
for document.all

Given the number of browsers implementing a document.all collection and
their otherwise divers DOMs the only valid inference that can be derived
form the existence of the document.all collection is that a document.all
collection exists.
and I
should not adres the pageYOffset via the .body part.

pageYoffset is a property of the global object, the body element has
nothing to do with it.
I don't know how to adres it then (I could not find an
example of that via google).

Or should it be like this:

IEroot = (document.compatMode!="BackCompat")?
document.documentElement : document.body;

Implemented values for document.compatMode include "BackCompat",
"QuirksMode" and "CSS1Compat", making a positive test for CSS1Compat
more viable than checking that the value is not equal to BackCompat. But
an - indexOf - test for "CSS" would achieve the same and maybe survey
some future implementation of "CSS2Compat" (if that happen, and assuming
that the root element would not be moved again).
And then
document.all ? IEroot.scrollTop : window.pageYOffset;
?

No, the document.all inference is not valid and window.pageYOffset
should be preferred and not fall-back.
:>And is it smart to replace:
:> document.body.scrollLeft
:>by
:> document.body.scrollLeft + document.documentElement.scrollLeft
:
:The root-element/document.compatMode problem is not addressed
:by this code.
So the answer is yes?

The answer to the question "is it smart to replace on piece of code that
does not take the compatMode into account with another that does not
take the compatMode into account?". No, the answer is no.
(sorry for asking confirmation, I'm not sure that
I do understand the answer correctly).

I wouldn't worry about it, with a context menu script the result will be
broken even when it is well implemented.

Richard.
 
D

DJ WIce

<snip>
: I wouldn't worry about it, with a context menu script the result will be
: broken even when it is well implemented.

LOL

Wouter
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top