Mouseposition in Netscape7

D

Dade Murphy

Hi everyone!

I've got a little problem and just cann't find any solution for it: I
need to get the current position of the mousecursor at my webpage for
some overlay stuff. Currently I use the following code for this:

if (ieDom)
{
// Internet Explorer
X=window.event.clientX + document.body.scrollLeft;
Y=window.event.clientY + document.body.scrollTop;
}
else if (nsDom)
{
// Netscape 4.xx
X=event.screenX + window.pageXOffset;
Y=event.screenY + window.pageYOffset;
}
else if(W3CDom)
{
// Netscape 6, 7, Mozilla, Opera, etc.
?????????
}

So... can anybody tell me what code to use for the NS6 part? screenX
is redefined in Gecko no longer being the position of the mousecursor
but the position of the browser window.

I would appreciate any help.
Dade
 
L

Lasse Reichstein Nielsen

Hi everyone!

I've got a little problem and just cann't find any solution for it: I
need to get the current position of the mousecursor at my webpage for
some overlay stuff. Currently I use the following code for this:

if (ieDom)

Don't switch on what you suspect is the dom. Just og for the
properties themselves. I usually use this:

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!).

Both Netscape 4 and Mozilla (and Opera 7) supports "event.pageX",
while IE supports the W3C DOM "clientX" property and the proprietary
"scrollLeft".


/L
 
D

Dade Murphy

Hi again!

Just tried the code you sent. Looks good with NS4 and IE, but still
doesn't work with NS6, NS7 and Mozilla. The mouseposition still
remains unset and the browser gets me some JS exceptions.

Would appreciate further ideas.
Dade
 
R

Richard Cornford

Lasse Reichstein Nielsen said:
(e-mail address removed) (Dade Murphy) writes:
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.
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top