"John" <vi****@REMOVEiprimus.com.au> writes:
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
--
Lasse Reichstein Nielsen -
lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'