Connecting Tech Pros Worldwide Forums | Help | Site Map

Capture Mouse Coordinates FireFox

John
Guest
 
Posts: n/a
#1: Dec 7 '05
Can Someone Please tell me why my script works in IE, but not in any other
browser>????
Thanks.


__________________________________________
var IE = document.all?true:false;

if (!IE) document.captureEvents(Event.onClick)
document.onClick = getMouseXY;

var tempX = 0;
var tempY = 0;

function getMouseXY(e) {
if (IE) { // grab the x-y pos.s if browser is IE
tempX = event.clientX + document.body.scrollLeft;
tempY = event.clientY + document.body.scrollTop;
}
else { // grab the x-y pos.s if browser is NS
tempX = e.pageX;
tempY = e.pageY;
}
if (tempX < 0){tempX = 0;}
if (tempY < 0){tempY = 0;}

somealert(tempX);
return true;
}

function somealert(tempX) {
window.alert(tempX);
}
_________________________________________



RobG
Guest
 
Posts: n/a
#2: Dec 7 '05

re: Capture Mouse Coordinates FireFox


John wrote:[color=blue]
> Can Someone Please tell me why my script works in IE, but not in any other
> browser>????
> Thanks.
>
>
> __________________________________________
> var IE = document.all?true:false;[/color]

You are assuming that any browser that has detectable support for
document.all also implements the IE event model - that doesn't seem like
a reasonable idea.

Why not test for the feature you are using?
[color=blue]
>
> if (!IE) document.captureEvents(Event.onClick)[/color]

if (document.captureEvents) document.captureEvents(Event.onClick);


Though I think that is completely unnecessary.

[color=blue]
> document.onClick = getMouseXY;
>
> var tempX = 0;
> var tempY = 0;
>
> function getMouseXY(e) {
> if (IE) { // grab the x-y pos.s if browser is IE[/color]

Ditch that line, use:

var e = e || window.event;

[color=blue]
> tempX = event.clientX + document.body.scrollLeft;
> tempY = event.clientY + document.body.scrollTop;
> }
> else { // grab the x-y pos.s if browser is NS
> tempX = e.pageX;
> tempY = e.pageY;
> }[/color]

Again, feature detection is the go:

if ('number' == typeof e.pageX){
tempX = e.pageX;
tempY = e.pageY;
} else if ('number' == typeof e.pageX){
tempX = e.clientX + document.body.scrollLeft;
tempY = e.clientY + document.body.scrollTop;
}

Assumes support for document.body.scrollLeft if e.pageX is supported.
You may wish to further test that.


[...]


--
Rob
Lasse Reichstein Nielsen
Guest
 
Posts: n/a
#3: Dec 7 '05

re: Capture Mouse Coordinates FireFox


"John" <visual@REMOVEiprimus.com.au> writes:
[color=blue]
> Can Someone Please tell me why my script works in IE, but not in any other
> browser>????[/color]

Because it is specifically written to work in IE 4 and Netscape 4.
Accidentally, it also works in later versions of IE.

The script is most likely 5+ years old, and makes assumptions that
were wrong even then (that IE and NS 4 are the only existing browsers).
Today, it's dangerously wrong.

[color=blue]
> var IE = document.all?true:false;[/color]

This suggests that the presence of document.all *guarantees* that the
browser is IE. That is incorrect.

Detecting the browser was never a good idea, since, as a strategy, it
is almost certain to fail when new browsers appear.

The recommended strategy is feature detection: check for the exact
feature you need, and don't try to infer it from a flawed browser
detection.

[color=blue]
> if (!IE) document.captureEvents(Event.onClick)[/color]

This line assumes that any non-IE browser is Netscape 4. Since that
is not the case, this line might give an error in browsers that
test as non-IE.

A safer way to support Netscape 4 (the only browser requireing
this line, and not one I would support at all) would be:

if (document.captureEvents && this.Event && Event.onClick) {
document.captureEvents(Event.onClick);
}
[color=blue]
> document.onClick = getMouseXY;[/color]

I am surprised that this works at all, since the "onclick" event
handler is written with without capital letters.
[color=blue]
> var tempX = 0;
> var tempY = 0;[/color]

Should these really be global variables?
[color=blue]
> function getMouseXY(e) {
> if (IE) { // grab the x-y pos.s if browser is IE
> tempX = event.clientX + document.body.scrollLeft;
> tempY = event.clientY + document.body.scrollTop;[/color]

again the assumption that anything that tests as IE (has a
document.all) will also implement IE's event handling, including the
global event object.

Instead do something like:

e = e || window.event; // support IE's global event object

if (typeof e.pageX == "number") {
tempX = e.pageX;
tempY = e.pageY;
} else if (typeof e.clientX == "number" &&
typeof document.body.scollLeft == "number") {
tempX = e.clientX + document.body.scrollLeft;
tempY = e.clientY + document.body.scrollTop;
} else {
// panic;
}


Generally, the script suffers from the flawed assumption that there
are exactly two browsers, and that document.all distinguishes them.

/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Closed Thread