Michael Winter wrote:[color=blue]
> On Fri, 12 Nov 2004 09:25:31 -0000, Danny@Kendal
> <danny@STOPSPAMghpkendal.co.uk> wrote:
>[color=green]
>> How can I get the coordinates of the mouse cursor in Mozilla browsers
>> as well as Opera and IE6?[/color]
>[/color]
The answer is that you need cross-browser code since MSIE 6 does not
support DOM 2 Events interface attributes and methods.
[color=blue]
>
> [snip]
>[color=green]
>> divX & divY are locally declared numerical variables in the test page.[/color]
>
>
> No, they aren't. They're globally declared.
>
> [snip]
>[color=green]
>> function movement()
>> {
>> divX = event.x
>> divY = event.y[/color]
>
>
> The first problem is that you're accessing the event object as though
> it's global. Whilst this might be true in IE, and browsers that emulate
> it for compatibility (like Opera), Netscape-emulating/DOM-conforming
> browsers must use a local event object. If the event listener is added
> directly, this object is passed as an argument. In intrinsic events
> added through HTML, there is an implicit local variable called "event".
> Quite conveniently really, as this allows for:
>
> ... onmousemove="movement(event);" ...
>
> which will work with both IE and conforming browsers.
>
> That could change your code to:
>
> function movement(evt) {
> var divX = evt.x,
> divY = evt.y;
>
> /* [write output] */
> }
>
> However, there's still one more problem: x and y aren't standard
> properties of the event object.[/color]
x and y event properties are coordinates relative to relatively
positioned elements in MSIE.
Interactive demo on event.x and event.y
http://www10.brinkster.com/doctorunc...ngEventXY.html
I've reopened an Opera bugfile 123298 on this btw.
http://www10.brinkster.com/doctorunc...a7EventXY.html
The W3C defines clientX/Y for[color=blue]
> coordinates within the browser viewport. You could get away with:
>
> var divX = evt.x || evt.clientX,
> divY = evt.y || evt.clientY;
>[/color]
The above does not make sense. clientX and clientY are well supported
event properties in browsers. x and y are not and are not very useful.
Even support for x and y properties were dropped in Mozilla (they were
supported in NS 4). And as said before, x and y are relative to
relatively positioned elements while clientX and clientY are relative to
browser window viewport (not to document)
[color=blue]
> but something more complex may be more reliable.
>
> var movement = (function(e) {
> /* The default getCoordinates (gC) function. If neither the
> * Microsoft or DOM approach is deemed supported, this will be
> * used to always return (0, 0).
> */
> function gC(e) {return {x: 0, y: 0};}
> /* The DOM getCoordinates (dC) function. */
> function dC(e) {return {x: e.clientX, y: e.clientY};}
> /* The Microsoft getCoordinates (mC) function. */
> function mC(e) {return {x: e.x, y: e.x};}[/color]
I doubt this mC function is really needed.
[color=blue]
> /* Tests if the given argument is a number. */
> function isN(o) {return 'number' == typeof o;}
>[/color]
isN is also not needed as the function isNaN is already available and
widely implemented in javascript browsers.
[color=blue]
> /* Check if the clientX and clientY event properties are
> * supported. If so, replace the default, gC, with dC.
> */
> if(isN(e.clientX) && isN(e.clientY)) {gC = dC;}
> /* If not, try again with the x and y properties and replace
> * gC with mC if successful.
> */
> else if(isN(e.x) && isN(e.x)) {gC = mC;}
>
> /* Now our testing is out of the way, replace the initial
> * function with a streamlined version and call it.
> */
> (movement = function(e) {
> var div = gC(e);
>
> /* [write output using div.x and div.y] */
>
> })(e);
> });
>[/color]
There may be (not sure; it depends on the design requirements) another
problem which is not addressed in the above code: when the page content
exceeds browser window viewport: the returned coordinates will not
return the coordinates in the document scroll view.
Interactive window properties, methods, events page
http://www10.brinkster.com/doctorunc...dowsMSIE6.html http://www10.brinkster.com/doctorunc...indowsNS6.html http://www10.brinkster.com/doctorunc...owsOpera7.html
DU
--
The site said to use Internet Explorer 5 or better... so I switched to
Mozilla 1.7.3 :)