Capture Mouse Coordinates FireFox

J

John

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);
}
_________________________________________
 
R

RobG

John said:
Can Someone Please tell me why my script works in IE, but not in any other
browser>????
Thanks.


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

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?
if (!IE) document.captureEvents(Event.onClick)

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


Though I think that is completely unnecessary.

document.onClick = getMouseXY;

var tempX = 0;
var tempY = 0;

function getMouseXY(e) {
if (IE) { // grab the x-y pos.s if browser is IE

Ditch that line, use:

var e = e || window.event;

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;
}

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.


[...]
 
L

Lasse Reichstein Nielsen

John said:
Can Someone Please tell me why my script works in IE, but not in any other
browser>????

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.

var IE = document.all?true:false;

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.

if (!IE) document.captureEvents(Event.onClick)

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);
}
document.onClick = getMouseXY;

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

Should these really be global variables?
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;

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
 

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,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top