"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:oe**********@hotpop.com...
da*********@gmx.at (Dade Murphy) writes:
<snip> var root = document.documentElement||document.body;
var pageX = event.pageX || event.clientX + root.scrollLeft;
var pageY = event.pageY || event.clientY + root.scrollTop;
Your code only uses document.body, which fails in IE in standards
mode (and you *should* write new pages to standards mode!).
I have mentioned before that your root element determining method is
wrong for IE 5.0 because it has enough of a DOM implementation to have a
documentElement but the root element is _always_ document body.
I don't think that code that needs to choose the root element has much
choice but to either take the line used in the FAQ and see if
documentElement has a non-zero client (or offset) Width/Height prior to
using it, or follow the, IMO better, technique of examining the
document.compatMode property for its existence and content.
Both Netscape 4 and Mozilla (and Opera 7) supports "event.pageX",
while IE supports the W3C DOM "clientX" property and the
proprietary "scrollLeft".
There is a (default) two pixel discrepancy between - event.pageX - and
event.clientX+root.scrollLeft - due to IE including the inner border of
the viewPort in its mouse co-ordinates. It can be corrected for by
subtracting root.clientLeft (and clientTop for Y) from the mouse posting
values (bringing the results into line with the co-ordinates use for
pageX and positioning elements on the page). But because some browsers
do not provide a clientLeft on the root element it would be safer to
default it to zero if undefined. Assuming a browser window size of less
than 2^31 pixels a binary NOT zero operation would be both quick and
safe:-
event.clientX + root.scrollLeft - (root.clientTop|0)
There is also a bug in Opera <= 6 where the clientX/Y values of the
event objects are equivalent to pageX/Y values on other browsers. But
Opera <= 6 does not provide pageX/Y so it has to be arranged that the
scrollLeft/Top and clientLeft/Top are not applied to Opera <=6. This is
the one occasion where I have seen a reason for using the window.opera
property as Opera 7 has PageX/Y so it takes the first option anyway.
Richard.