window.event good for IE but not Netscape/Firefox

R

RC

I am try to detect the mouse pointer by

var event = window.event;


var x = event.pageX;
var y = event.pageY;

This is working fine in IE, but in
Netscape/Firefox where event return undefined.

Can someone tel me how to detect mouse pointer
in Netscape/Firefox?

Thank Q very much in advance!
 
N

net.leandro

Utilizo da seguinte maneira

var ns4 = (document.layers) ? true : false ;
var ie4 = (document.all) ? true : false;


function ColumnOnMouseMoveEvent (e){
var mouseX = 0;
mouseX=ie4? event.clientX : e.clientX;
var mouseY = 0;
mouseY=ie4? event.clientY : e.clientY;

}
 
A

ASM

RC a écrit :
I am try to detect the mouse pointer by

var event = window.event;


var x = event.pageX;
var y = event.pageY;

This is working fine in IE, but in
Netscape/Firefox where event return undefined.


function coords(e){
var x = e.pageX? e.pageX : 0;
var y = e.pageY? e.pageY : 0;
alert('x = '+x+'\ny = '+y);
}

<a href="#" onclick="coords(event)">event</a>



<html>
<script type="text/javascript">
function mousecoords(e){
var x = e.pageX? e.pageX : 0;
var y = e.pageY? e.pageY : 0;
document.getElementById('coord').innerHTML='x = '+x+' y = '+y;
}
</script>
<body onmousemove="mousecoords(event);">
<p id="coord"></p>
</html>


http://www.quirksmode.org/viewport/compatibility.html
 
R

Randy Webb

(e-mail address removed) said the following on 5/8/2006 3:37 PM:
Utilizo da seguinte maneira

var ns4 = (document.layers) ? true : false ;
var ie4 = (document.all) ? true : false;

And what about NS6/7/8, Mozilla, Firefox, Opera 5/6/7/8, or any other
browser that doesn't support document.all or document.layers or that
does support them but not e.clientX (in the case of document.all)?

Don't assume that one test implies other features. Test for the features
you want to use directly.

mouseX = (e.clientX)? e.clientX : event.clientX;
mouseY = (e.clientY)? e.clientY : event.clientY;
 
R

RobG

RC said:
I am try to detect the mouse pointer by

var event = window.event;


var x = event.pageX;
var y = event.pageY;

This is working fine in IE, but in
Netscape/Firefox where event return undefined.

Can someone tel me how to detect mouse pointer
in Netscape/Firefox?

Thank Q very much in advance!



Read about getting mouse co-ordinates (and events in general) here:

<URL:http://www.quirksmode.org/js/events_properties.html>


Based on the above reference, to get the cursor's co-ordinates from an
event:

function someFn(e)
{
var e = e : window.event;
var posx = 0;
var posy = 0;
if (e.pageX || e.pageY) {
posx = e.pageX;
posy = e.pageY;
} else if (e.clientX || e.clientY) {
posx = e.clientX + document.body.scrollLeft;
posy = e.clientY + document.body.scrollTop;
}
// posx and posy contain the mouse position relative to the document
// Do something with this information
}


And in the html:

<... onclick="someFn(event);" ...>
 
L

Lasse Reichstein Nielsen

var ns4 = (document.layers) ? true : false ;

Detects true for Netscape 4 or OmniWeb (and isn't used here)
var ie4 = (document.all) ? true : false;

Detects true for IE or Opera or probably a few more.
function ColumnOnMouseMoveEvent (e){

A less fragile solution, that doesn't involve detecting the browser,
is to start the function with:
e = e || window.event;

If the event is passed as a parameter to the function (as Netscape and
W3C DOM Events compatible browsers do) this statement does nothing.

If not, it assumes that the event is available as a global variable
(as in IE).

To my knowledge, there is no browser supporting Javascript and events
that does neither.

var mouseX = e.clientX;
var mouseY = e.clientY;

/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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top