469,281 Members | 2,465 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,281 developers. It's quick & easy.

window.event good for IE but not Netscape/Firefox

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!
May 8 '06 #1
6 14229
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;

}

May 8 '06 #2
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
--
Stephane Moriaux et son [moins] vieux Mac
May 8 '06 #3
ne*********@gmail.com 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;

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 8 '06 #4
Randy, I agree to you

May 8 '06 #5
RC wrote:
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);" ...>
--
Rob
May 8 '06 #6
ne*********@gmail.com writes:
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
--
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.'
May 10 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Frances Del Rio | last post: by
4 posts views Thread by Andrew Poulos | last post: by
3 posts views Thread by jeff29_b | last post: by
8 posts views Thread by David Gravereaux | last post: by
26 posts views Thread by yb | last post: by
18 posts views Thread by len.hartley | last post: by
10 posts views Thread by Andrew Neiderer | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.