Mozilla & Mouse coordinates

G

Guest

How can I get the mouse coordinates in Mozilla?

I try
x = event.pageX
but it does not work...
basically I try
alert(event) and it does not work.
Where is the problem?
the full code
------------------------
<script>
var obj = document.getElementById('tooltip');
if (navigator.appName == "Microsoft Internet Explorer") isIE = true; else
isIE = false;

function handlerMM(e){
if (isIE) { x = event.pageX; y = event.pageY; }
else {
alert(event);
x = event.clientX + document.body.scrollLeft;
y = event.clientY + document.body.scrollTop;
}
obj.style.top = y + 10;
obj.style.left = x + 15;
}
</script>
 
L

Lee

How can I get the mouse coordinates in Mozilla?

I try
x = event.pageX
but it does not work...
basically I try
alert(event) and it does not work.
Where is the problem?
the full code
------------------------
<script>
var obj = document.getElementById('tooltip');
if (navigator.appName == "Microsoft Internet Explorer") isIE = true; else
isIE = false;

function handlerMM(e){
if (isIE) { x = event.pageX; y = event.pageY; }
else {
alert(event);
x = event.clientX + document.body.scrollLeft;
y = event.clientY + document.body.scrollTop;
}
obj.style.top = y + 10;
obj.style.left = x + 15;
}
</script>

In Mozilla and other browsers, the Event object isn't a global
variable. It's an argument that's passed to the event handler.
In your example, it's probably "e".
 
K

Keith Bowes

function handlerMM(e){
if (isIE) { x = event.pageX; y = event.pageY; }
else {
alert(event);

"event" is undefined. An instance of the Event interface is passed as
the argument e.
x = event.clientX + document.body.scrollLeft;
x = e.screenX;
y = event.clientY + document.body.scrollTop;

y = e.screenY;
}
obj.style.top = y + 10;
obj.style.left = x + 15;
}

A unit must be concatenated to the length. For example, y + 10 + 'px'
 
L

Lasse Reichstein Nielsen

How can I get the mouse coordinates in Mozilla?

The eight way! (as opposed to how you do it in IE :)
I try
x = event.pageX
but it does not work...

Sure it does, if the variable "event" refers to the event.
In IE, "event" is a global variable that always refer to the
current event, but that is an IE invention, and not all other
browsers have emulated it.

The type attribute is required in HTML 4.
var obj = document.getElementById('tooltip');
if (navigator.appName == "Microsoft Internet Explorer") isIE = true; else
isIE = false;

Browser detection is not a very good way to deduce Javascript
functionality. Partly because browsers change between versions, and
partly because some browsers lie about their name.
function handlerMM(e){

The argument, "e", given to the function when it is called, is the
event.
I usually start my handlers like this:

function handlerMM(event) {
event = event || window.event; // IE sucks!

(yes, I usually write the comment too!).
After this, you can use "event" to refer to the event in all browsers.
if (isIE) { x = event.pageX; y = event.pageY; }

I guess you mean 'if (!isIE)', becuase pageX and pageY are not
available in IE, but are in Mozilla. It would be more prudent to check
for "pageX" directly:

if (typeof event.pageX == "number") {
x = event.pageX;
y = event.pageY; // we dare expect that pageY exists if pageX does :)
}
else {
alert(event);
x = event.clientX + document.body.scrollLeft;
y = event.clientY + document.body.scrollTop;

If IE is in standards mode (and it *should* be!), then the html element
is the root of the document tree, not body. In that case, you need to
use document.documentElement instead of document.body.

It's easier jo just use whatever is there, instead of trying to figure
out whethere the page is begins shown according to standards.

var root = document.documentElement || document.body;
x = event.clientX + root.scrollLeft;
y = event.clientY + root.scrollTop;
obj.style.top = y + 10;

This fails in browsers that follow the standard. CSS lengths must have
a unit, and you are only assigning a pure number.
obj.style.top = (y + 10) + "px";
obj.style.left = x + 15;

ditto.

/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

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top