Get mouse coordinates

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Get mouse coordinates

Post by pilau »

How do I get the mouse coords?
I tried "window.event.clientX" and "window.event.clientY" - itworks on IE6 but it won't work on FireFox.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Not tested but something like this has a workaround.

Code: Select all

function foo(e)
 {
	if (window.Event)
	 {
		x = e.screenX;
		y = e.screenY;
	 }
	else
	 {
		x = window.event.clientX;
		y = window.event.clientY;
	 }
 }
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

should probably do a typeof test to see if the object exists or not..
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

Bah.. didn't work on FF.
Well drop it I'm changing the design and layouy anyway. Thanks.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

This works in both IE 6.0 and FF 1.0.7

Code: Select all

<html>
<head>
<script type="text/javascript">
function foo1(scrObj, e)
 {
        e = e || window.event;
        var id1 = document.getElementById("id1");

        var x = e.screenX || e.clientX;
        var y = e.screenY || e.clientY;

        id1.innerHTML = x + ", " + y;
 }
</script>
</head>
 <body>
 <div id="id1" style="border:1px solid black;width:250px;height:250px" onmousemove="foo1(this, event)">
 Test Contents
 </div>
 </body>
</html>
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

anjanesh wrote:This works in both IE 6.0 and FF 1.0.7
I promise I'll check it out :)
Post Reply