Yesterday while working on a script and I wanted to know the cursor X, Y coordinates. Suprisingly I forgot how to get the X and Y coordinates of mouse. After thinking for some time, its strike that everything related to the current state is stored in events. So How to get the X and Y positions.
-capture mousemove event and list down all its properites
This is how I find X and Y coordinates
<html>
<head>
<title>Get Mouse Coordinates</title>
<script language=”javascript”>
var divObj;
/**
* capture mousemove event, this statement will cause browser to
* call getMouseCoordites function each time mouse moves
*/
document.onmousemove=getMouseCoordinates;
/**
*identify which event is supported
* Based on that collect pageX and pageY properties of the event object
* pageX and pageY gets the X and Y cursor coordinates
*/
function getMouseCoordinates(event)
{
ev = event || window.event;
divObj.innerHTML = “Mouse X:”+ev.pageX + ” Mouse Y:”+ev.pageY;
}
//assign the mouseCoord Object to divObj
function loadDiv()
{
divObj = document.getElementById(“mouseCoord”);
}
</script>
</head>
<body onLoad=”loadDiv()”>
<div id=”mouseCoord”>Mouse Coordinates position will be displayed here.
</div>
</body>
</html>
Script works well with Mozilla, For IE need to do further modification in finding the exact position by calculating different properties value.
April 13, 2007 at 10:35 am |
Test
function unLoadFnc(evt)
{
var e = (window.event) ? window.event : evt;
alert(e.clientX);
}
Sample Html.
July 29, 2007 at 4:57 pm |
Mouse
yeah thats what I ment, i didnt remember the spelling but i did remember the meaning of the Mouse
April 12, 2008 at 2:31 pm |
Thank you So much for this script