By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,600 Members | 1,887 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,600 IT Pros & Developers. It's quick & easy.

Mozilla & Mouse coordinates

P: n/a
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>
Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
<- Chameleon -> wrote:

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'

Jul 20 '05 #2

P: n/a
"<- Chameleon ->" <ch******@hotmail.NOSPAM.com> writes:
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.
<script>
The type attribute is required in HTML 4.
<script type="text/javascript">
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
--
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.'
Jul 20 '05 #3

P: n/a
Lee
<- Chameleon -> said:

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".

Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.